OBJECTIVES
Illustration of the Concept of Control Flow
Types of Control Flow
Knowledge about conditional and repetitive
statements.
Make more proficient in handling programs
in Control Flow.
Develop Logic building skill.
3
Flow is the term used for the order that
the computer executes the lines of
codes in a program.
In any programming language three
types of control flow occurs.
1.Sequence
2.Selection
3.Iteration
4
1. SEQUENCE CONSTRUCT
In this type of construct the statements
are executed sequentially.
i.e. Statement1 followed by Statement
2 , and Statement 2 followed by
Statement3 and so on.
Statement 1
Statement 2
Statement 3
5
2. SELECTION CONSTRUCT
Selection construct means – execution of
statement(s) depending upon a condition test.
If a condition evaluates to true, a course of
action will be followed (set of statements)
otherwise another course of action (different
set of action ) will be followed.
This is also called as Conditional Construct
or Decision Construct.
6
TRUE
Condition
?
Statement 1 Statement 2
Statement 1
Statement 2
One course-of-action
FALSE
Another
course of
action
7
Selection Statements
If Statement
Switch Case
Note: In certain circumstances , conditional operator called as
ternary operator (? :) is used as an alternative to IF
statement.
8
3. ITERATION/ REPETITIVE
CONSTRUCT
Iteration Construct means repetition of a set of statements
depending upon a condition test.
Till the condition is true a set of statements are repeated
again and again.
As soon as the condition becomes false, the iteration
stops.
This construct is also called as Looping Construct.
The set of statements that are repeated again and again is
called the body of the loop. The condition on which the
execution or exit of loop depends is called exit condition
or test condition.
9
The Iteration Construct
FALSE
Condition
?
Statement 1
Statement 2
TRUE
The Loop Body
The exit Condition
10
Iteration Statements
For Loop
While Loop
Do- While Loop
12
Conditional Statements
1. If Statement
Syntax
if (expression)
statement1
else
statement2
13
Program –
Write a Temperature Conversion Program in C++ that gives user the
option of converting Fahrenheit to Celsius or Celsius to Fahrenheit.
#include<iostream.h>
void main()
{
int choice;
float temp, countemp;
cout << “Temperature Conversion Menu” <<“\n”;
cout << “1. Fahrenheit to Celsius” <<“\n”;
cout << “2. Celsius to Fahrenheit” <<“\n”;
cout << “Enter your choice. Press either 1 or 2” ;
cin >>choice;
if (choice ==1)
{
cout << “\n” <<“Enter Temperature in Fahrenheit :”;
cin >>temp;
countemp = (temp-32)/1.8;
cout <<“\n” <<“ The temperature in Celsius is : ” << countemp <<“\n”;
}
else
{ cout << “\n” <<“Enter Temperature in Celsius :”;
cin >>temp;
countemp = 1.8 * temp + 32;
cout <<“\n” <<“ The temperature in Fahreheit is : ” << countemp <<“\n”;
}
14
OUTPUT - When user choice is 1
Temperature Conversion Menu
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice : 1
Enter temperature in Fahrenheit : 98
The temperature in Centigrade is : 36.66
15
OUTPUT – When user choice is 2
Temperature Conversion Menu
1. Fahrenheit to Celsius
2. Celsius to Fahrenheit
Enter your choice : 2
Enter temperature in Centigrade : 37
The temperature in Fahrenheit is : 98.6
16
Nested Ifs
Syntax
if (expression1)
{
:
if(expresssion2)
statement1;
[ else
statement2;]
:
}
else
body of else;
if (expression1)
{
body of if
if(expresssion2)
statement1;
[ else
statement2;]
:
}
else
body of else;
Part shown in [] is
optional
Minimum Level of Learning Questions
Write a program to check if a number is
even or odd.
Write a program to check if a person is
eigible for vote or not.
HOTs
Write a program to print largest of three
numbers.
17