Professional Documents
Culture Documents
Chapter 3 Part - 2
Chapter 3 Part - 2
Chapter (3)
1
Contents
switch statement
conditional operator
Loops
The "for” Loop
Three Expressions in the “for” Loop
Multiple statements in loop body
for Loop Variations
Objectives
To learn syntax of switch, and conditional operator
To know how the length of code and memory space are
reduced using loops
To understand about “for” loop
switch Statement
If all the decisions depend on the same variable, consider switch
statement instead of if…else or else if constructions
case 1:
statement1; switch variable
equals second
True
Second case body
statement2; ……
break;
………….. False
Default body
default: Exit
statement;
}
no semicolon here Figure 1: Operation of the switch
statement
switch Statement (Example-1)
Syntax:
variable = (test expression) ? expression1 : expression2;
Conditional Operator
Test expression
is evaluated each time through the
loop before executing the loop
Test True
determine whether the loop will expression
be executed again Body of loop
decrementing it
always executed at the end of the Figure 2: Operation of the for loop
loop
For Loop (Example-1)
Table 1: Trace of for loop
// displays the message 5 times Step 1 Step 2 Step 3 Step 4
#include<iostream.h> (Initialization) i (Test)
i<5
(Body of Loop)
(Increment)
i++;
using namespace std;
i=0 0 0<5 (true) Introduction to for loop 1
int main()
{ 1 1<5 (true) Introduction to for loop 2
Initialization expression
2 2<5 (true) Introduction to for loop
int i; Test expression
3
60
120 fact *= j;
Syntax:
for ( Initialization; Test; Increment/Decrement )
{
outer loop
for (Initialization; Test; Increment/Decrement )
Inner loop
{
statement(s);
}
statement(s); // you can put more statements.
}
Nested for Loop (Example-1)
2 True
3 True **** Output
cout<<"*"; 4 True *****
: : False *****
: : *****
cout<<endl; 5 (exit from
inner loop)
*****
} : : : : :
*****
return 0; 4
False
(exit from
} outer loop)
Nested for Loop (Example-2)
cout<<"*"; ***
i=3, j=0,1,2,3
cout<<endl; ****
}
Nested for Loop (Example-3)
// displays multiplication tables
Output
1*1=1
int i,j,k,n; 1*2=2
1*1=1
n 1*3=3
cout<<"Enter the number of tables : "; 1*2=2
1*3=3
1*4=4
2
cin>>n; 1*4=4
1*5=5
1*5=5
1*6=6
for(i=1; i<=n; i++) //outer loop 1*6=6
1*7=7
1*7=7
1*8=8
{ 1*8=8
1*9=9
1*9=9
1*10=10
for(j=1; j<=10; j++) // inner loop 1*10=10
{ 2*1=2
i 2*2=4
k= i * j ; 2*3=6
i 2*4=8
cout<< i << "*" << j << "=" << k <<endl; 2*5=10
2*6=12
} 2*7=14
2*8=16
cout<<endl; 2*9=18
} 2*10=20
Multiple Initialization and Test Expressions
#include<iostream>
using namespace std;
int main()
{
Output
int j, a, b=10;
30
Multiple initializations Multiple increments/decrements 29
28
27
26
for( j=0, a=20 ; j<5 ; j++, b-- )
cout<<a+b<<endl;
return 0;
}
Multiple Initialization and Test Expressions)
(Cont’d)
can leave out some or all of the expressions
for( ; ; )
cout<<“Hello”;
will print out infinite numbers of “Hello”
int i=0;
for( ; i<5; i++)
cout<<“Hello”; will print out 5 times-
“Hello”
Should avoid using such multiple or missing expressions which can lead to
decrease its readability
Summary
In this lecture, you will learn:
switch provides one selection from many alternatives, evaluated and compared to each
case value.
conditional operator that can be used to replace “if...else” statements.
Loop is used when you need to execute a block of code several number of times.
Three types of loops: for, while and do-while loop
The for loop handles details of the counter-controlled loop that provides a compact way to
iterate over a range of values.
The general form of the for statement can be expressed by three expressions.
Nested for loop: a for loop can be nested completely contained within another for loop.
Multiple initializations and text expressions can be possible.
References
Farrell
5. C Programming for Engineering & Computer Science by H.H. Tan and T.B.
D’Orazio
Thank You!
27