IIIT Bhubaneswar: Dept. Comp. Sc. & Engg. C++ and Object Oriented Programming Flow of Control
IIIT Bhubaneswar: Dept. Comp. Sc. & Engg. C++ and Object Oriented Programming Flow of Control
IIIT Bhubaneswar
Dept. Comp. Sc. & Engg.
C++ and Object Oriented Programming
Lecture: 10, 11
Flow of Control
I. Control structure II. Flow Chart Representation
Lecture: 10:
I. Control Structure:
While writing a program, sometimes the programmer may require to alter the
flow of execution based on certain condition or to perform the same operation
repeatedly.
In such situation the programmer uses the control structure.
A programmer may use one or any combination of the following three control
structure:
Sequence Structure (straight line)
Selection Structure (branching)
Loop Structure (iteration or repetation)
Control
Sequence Structure:
Example:
int main()
{
int x, y, sum,avg;
cout<<”Enter the values for x and y”;
cin>>x>>y;
sum=x+y;
avg=sum/2 ;
cout<<”The value of sum is:”<<sum;
cout<<”The value of average is:”<<avg;
return 0;
}
In this program, the flow of control is sequential from one statement to the
next one.
Selection Structure:
In selection structure, the decision making statements are used.
The if statement
The if-else statement
The if-elseif-else statement
The nested if-else statement
The switch case statement
The nested switch case statement
Along with these we make use of statements like
break
continue
goto
Syntax: Syntax:
I. if statement II. if else statement
if (expression) if (expression)
{ If true { If true
statement1; statement 1;
statement2; } If false
} else
statement; {
... statement 2;
... }
CSE/C++/Module-I/Lecture: 10, 11 3
Syntax: Syntax:
III. if else if statement IV. nested if else statement
if (expression 1) if (expression 1)
{ If true {
statement 1; if (expression 2)
} {
elseif (expression 2) statement 1;
{ If true }
statement 2; else
} {
elseif(expression 3) statement 2;
{ If true }
statement 3; }
} else
else {
{ If false if (expression 3)
statement 4; {
..... statement 3;
} }
else
{
statement 4;
}
}
Syntax:
V. switch case statement
switch (expression)
{ Floating type not allowed (only integer and character t
case 1:
{
statement 1;
....
break;
}
case 2:
{
statement 2;
....
break;
}
default:
statement 3;
}
Lecture: 11:
Loop Structure:
CSE/C++/Module-I/Lecture: 10, 11 4
While Loop
For loop
In entry control condition is checked first, if it is true action is performed else
not.
Exit Control
Do While Loop
In exit control loops, the condition is checked after the statement is
executed (once), if condition is true then action is repeated else not.
Syntax:
I. The while Loop: If true then it enters the block and executes the statement. Then c
while (condition)
{
statement 1;
statement 2;
.....
}
statement;
...
If condition is false from the beginning then the block is not executed
at all.
Syntax:
II. The do while Loop:
do
{
statement 1;
statement 2;
.....
} while (condition)
statement;
...
Execute the block at least once. Then checked the condition.
Syntax:
III. For Loop: 1 2 4
for(initialization; condition; modification)
{
..... When condition is true
statements;
3
.....
}
statements;