Вы находитесь на странице: 1из 32

CSC103: Introduction to Computer

and Programming
Lecture No 9

1
Previous Lecture
• Nested if else statement
• Logical operator in C
• How logical operator can be used to make
compound conditions
• Conditional operator

2
Today’s lecture outline
• Nested conditional operator
• Example programs –
– nested if-else
– nested conditional operator
• Loops
– while loop

3
Data Types
• data type or simply type is a classification
of data which tells the compiler or interpreter how the
programmer intends to use the data.

• C++ data can be classified into three categories:

– Simple data type

– Structured data type

– Pointers

4
Simple Data Types
• Three categories of simple data

– Integral: integers (numbers without a


decimal)

– Floating-point: decimal numbers

– Enumeration type: user-defined data type

5
6
int Data Type
• Examples:
-6728
0
78
• Positive integers do not have to have a + sign
in front of them
• No commas are used within an integer
• Commas are used for separating items in a list

7
bool Data Type
• bool type

– Has two values, true and false

– Manipulate logical (Boolean) expressions

• true and false are called logical values

• bool, true, and false are reserved words

8
char Data Type

• The smallest integral data type


• Used for characters: letters, digits, and special
symbols
• Each character is enclosed in single quotes
• Some of the values belonging to char data type
are: 'A', 'a', '0', '*', '+', '$', '&'
• A blank space is a character and is written ' ',
with a space left between the single quotes
9
Floating-Point Data Types
• C++ uses scientific notation to represent real
numbers (floating-point notation)

10
Floating-Point Data Types (continued)
• float: represents any real number
– Range: -3.4E+38 to 3.4E+38
• Memory allocated for the float type is 4
bytes
• double: represents any real number
– Range: -1.7E+308 to 1.7E+308
• Memory allocated for double type is 8 bytes
• On most newer compilers, data types
double and long double are same

11
Nested conditional operator
(expression 1 ? expression 2 : expression 3);

Single
condition or (expression 1 ? expression 2 : expression 3);
compound
condition

(expression 1 ? expression 2 : expression 3);

12
Example program
main( )
{
float sal ;
cout<<"Enter the salary" ;
cin>> "%f", &sal ) ;
if ( sal < 40000 && sal > 25000 )
cout<< "Manager" ;
else
if ( sal < 25000 && sal > 15000 )
cout<< "Accountant" ;
else
cout<< "Clerk" ;
}
13
Cont.
main( )
{
float sal ;
cout<<"Enter the salary" ;
cin>> "%f", &sal ;
( (sal < 40000 && sal > 25000) ? cout<< "Manager" :
(( sal < 25000 && sal > 15000 ) ? cout<< "Accountant" :
cout<< "Clerk" ));
}

14
Example program 1
• Three numbers x, y, z are input through the
keyboard, write a program to determine the
smallest of the three.
Write a program

15
Example program 2
• Any character is entered through the
keyboard, write a program to determine
whether the character entered is a capital
letter, a small case letter, a digit or a special
symbol. Write a program

16
Loops in C
• Three major loops structures in C
– while loop
– for loop
– do while

17
Loop in general
• Loop has a termination point  finite loop
– Loop execution stops when the loop
condition becomes false
• Loop has a counter that counts number of
iterations of that loop
• Loop has a statement or a set of statements
that are executed until the loop condition
become false

18
Repetition - Example
• Formula 1 car race
• There is a path/track
• Each car has to complete a
certain no of rounds say 10
• In each round, when a car cross
the finish line
– the condition is check whether
the car has completed total no
of round or not.

19
General form while loop

20
The while loop

Condition

Set of
statements Loop counter

21
A while loop program
main( )
{ Memory
int count = 0;
int total = 0; count 2
34
05
1
while(count < 5)
{ total 103
1
0
6
total = total + count;
printf(“count =%d, total = %d”, count, total);
count = count +1;
}
}
Program Output
count = 0, total = 0
count = 1, total = 1
count = 2, total = 3
count = 3, total = 6
count = 4, total = 10 22
Points to remember
• Loop body will keep on executing until the
loop condition become false
• When loop condition become false, the first
statement after the while block will be
executed
• Condition can be a single or compound

23
Cont.
• Statement within loop body can be single line
or block of statement.
• In case of single line parentheses are optional

• In case of block, parentheses are must


24
Cont..
• A loop can be infinite loop

25
Cont.
• Loop counter can be decremented

26
Cont.
• It is not necessary that a loop counter must
only be int. it can be float or char

27
Pre and post increment operator
• Pre increment/ decrement
– ++x; is same as x = x + 1;
– or – –x is same as x = x – 1;
• Post increment/ decrement
– x++; is same as x = x + 1;
– or x-- is same as x = x – 1;

28
Difference between pre and post operators
x = 1;
y = x;
y = x ++;
x = x + 1;
printf(“x = %d, y = %d ”, x, y);

output
x = 2, y = 1

x = 1; x = x + 1;
y = ++ x; y = x;
printf(x = %d, y = %d ”, x, y);

output
x = 2, y = 2 Write a program

29
Other operator

i = i + 1;
• += is a compound assignment operator
• j = j + 10 can also be written as j += 10
• Other compound assignment operators are -=, *=, / =, %=.

30
Example program
First comparison i < 10 is
performed then value of
i is incremented

First the value of i is


incremented then
comparison i < 10 is
performed

31
32

Вам также может понравиться