You are on page 1of 27

Introduction to Programming

Week # 3

1 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Agenda for Today
 Basic Building Blocks
 Character Set
 Tokens
 Keywords
 Identifiers
 Constants
 Variables
 Literals
 Data Types
 Operators

2 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Basic Building Blocks

 A set of rules, symbols, and special words used to


construct programs.

3 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Character Set

 A set of valid characters that a language can recognize


 Letters: A-Z, a-z
 Digits: 0-9
 Special Characters: Space, +  - * / ^ \ () [] {} = != <>  ‘ 
“  $  ,  ;  :  %  !  & ? _  #  <=  >=  @
 Formatting Characters: newline, backspace, horizontal
tab, vertical tab, form feed, and carriage return

4 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Tokens

 A token is a group of characters that logically belong


together
 Keywords
 Identifiers
 Constants
 Variables
 Literals
 Data Types
 Operators

5 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Tokens

 Keywords: these are some reserved words in C++


which have predefined meaning to compiler
 Example: if, else, for, while etc.

6 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Token

 Identifier: a sequence of one or more letters, digits, or


underscore characters (_)
 An identifier can consist of alphabets, digits and/or
underscores.
 It must not start with a digit
 C++ is case sensitive that is upper case and lower case
letters are considered different from each other.
 It should not be a reserved word.

7 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Token

 Literals: data items that never change their value


during the execution of the program.
 Integer-constants
 Character-constants
 Floating-constants
 String-constants

8 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Integer-constants

 Whole number without any fractional part


 Decimal integer constants: sequence of digits and
should not begin with 0 (zero). For example 124, - 179,
+108.
 Octal integer constants: sequence of digits starting with
0 (zero). For example. 014, 012. 
 Hexadecimal integer constant: sequence of digits
preceded by 0x or 0X.

9 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Character-constants

 Contain one or more characters and must be enclosed


in single quotation marks
 For example 'A', '9', etc
 Non-graphic characters: cannot be typed directly from
keyboard
 backspace, tab, carriage return

10 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Floating-constants

 Numbers having fractional parts


 For example, 3.0, -17.0, -0.627, 1.2E2, 1.2e2 etc.

11 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


String-constants

 A sequence of characters enclosed within double


quotes is called a string literal
 For example: “Hello World”

12 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Data Types

 Basic data types


 Integer
 Float
 Character

13 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Data Types

Type Description
int Small integer number
long int Large integer number
float Small real number
double Double precision real number
long double Long double precision real number
char A single character

14 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Variable

 A variable is a location in the computer's memory


where a value can be stored for use later in a program.
 A variable has:
 Name
 Type
 Size
 Value

15 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Variable Declaration

 Sets aside memory for a variable


 Example: int x;

16 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Operators

 Arithmetical operators
 Relational operators
 Logical operators
 Unary operators
 Assignment operators
 Conditional operators
 Comma operator

17 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Arithmetic Operators

 Arithmetic operators +, -, *, /, and % are used to


performs an arithmetic (numeric) operation
 +, -, *, and / are allowed with both integer and float
types
 Modulus or remainder % operator is used only with
the integer data type.
 Operators that have two operands are called binary
operators

18 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Assignment Operator

 The assignment operator '=' is used to assign value to


a variable
 Takes the expression on its right-hand-side and places
it into the variable on its left-hand-side
 For example:
int x;
x = 10;

19 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Assignment Operator

int x, y;
x = 100;
y = x;

20 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Assignment Operator

int x, y, z;
x = 100;
y = z = x;

21 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Assignment Operator

 What is the outcome of the following?


int x, y, z;
x = 100;
y = 200;
z = x;
x = y;
y = z;
 Swapping Two Variables

22 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


A Sample Program (1)
 Assignment: write a C++ program for the following
problem.
 Compute net salary for a tax rate of 30%:
 Hint: Net Salary = Gross Salary – Total Tax

23 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


A Sample Program (2)
 Assignment: write a C++ program for the following
problem.
 Compute area of a circle for a given radius R.
 Hint: Area = Pi * R2

24 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


Operators

 Arithmetical operators
 Relational operators
 Logical operators
 Unary operators
 Assignment operators
 Conditional operators
 Comma operator

25 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


References

1. http://www.cppforschool.com/tutorial/basic.html

26 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023


 Constants, Variables, Data types, Operators
 A representative example program that covers all the
concepts discussed above.
 An other example to ask the students to test their
concepts.

27 Dr. Zakira Inayat, CS&IT, UET Peshawar 03/07/2023

You might also like