Control Structures in C++
Control Structures in C++
D.ARUN SHUNMUGAM
Asst. Prof. / ECE
Contents
Control Structures
Sequence Structure
Selection Structure
If Statement
If-else statement
Nested if-else statement
If-else ladder
Iteration Structure
For loop
While loop
Do-while loop
Control Structures:
Flow of execution of the program is called control struc-
ture.
In all programs could be written in terms of three control
structures,
1. Sequence Structure
2. Selection Structure
3. Iteration Structure
1. Sequence Structure
The sequence structure, the statements are being exe-
cuted sequentially.
It is the default flow of every program.
Execution starts from the first instruction and all the in-
structions are executed one by one in sequence.
Example:
2. Selection Structure (or) Conditional Statements:
The selection structure, that performs different processes
based on whether a condition is true or false.
Selection structures use relational operators to test condi-
tions.
If statement
If else State-
Types ment
Nested if else
Statement
If else ladder
if statement
It is used to control the flow of execution, whenever the
condition is true or false.
Syntax:
if(condition)
{
----
----
}
Properties of if statement:
1. If the condition is true the certain statements are exe-
cuted.
2. If the condition is false it does not do anything.
3. Condition must be placed in parenthesis.
Example Program:
#include<iostream.h>
void main()
{
True Output
Output
int i = 10;
False II am
am not
not in
in if
if
if (i > 15)
{
cout<<“10 is less than 15";
}
cout<<“I am not in if”;
}
if else statement
It is a two way decision making statement. If the condi-
tion is true execute some statements, if the condition is false ex-
ecute some other statements.
Syntax:
if(condition)
{
True Statements;
}
else
{
False Statements;
}
Example Program:
#include<iostream.h>
void main()
{
int i = 20; True Output
Output
False
if (i < 15) ii is
is greater
greater than
than 15
15
{
cout<<"i is smaller than 15";
}
else
{
cout<<"i is greater than 15";
}
}
nested if else statement
In this nested if else statement we can write an entire if
else statement in another if else statement is called nesting and
the statement is called nested if.
Syntax:
if(condition 1)
{
if(condition 2)
{ else
True Statement 2; {
} False Statement 1;
else }
{
False Statement 2;
}
}
if else ladder
Nested if else statement can become quite complex. If
more than three alternative is not possible in nested if else state-
ment in this situation, we use if-else ladder.
Syntax:
if(condition 1) else if(condition 3)
{ {
statement 1; Statement 3;
} }
else if(condition 2) else
{ {
Statement 2; default statement;
} }
Example Program:
#include<iostream.h>
void main()
{
int i = 20;
if (i == 10) Output
Output
cout<<"i is 10"; ii is
is 20
20
else if (i == 15)
cout<<"i is 15";
else if (i == 20)
cout<<"i is 20";
else
cout<<"i is not present";
}
3. Iteration Structure (or) Looping Statements
The Looping program consists of two parts,
1. Body of the loop
2. Control statement
The looping statement would include the following steps,
1.Initialization of condition variable.
2.Test the control statement.
3.Executing the body of the loop depending on the condition.
4.Updating the condition variable.
In C++ programming language there are three types
of loops;
For loop
Do..while loop
For loop
It is a repetitive control structure. It is used to execute set
of instruction repeatedly.
Syntax:
for ( initialization ; condition ; upda-
tion )
{
Body of the loop;
}
Example Program:
Printing numbers from 1 to 5.
#include<iostream.h>
void main() Output
Output
1
1223
3445
5
{
int i; // Variable Declaration
for (i = 1; i <= 5; ++i) Here
i=1 -> Initialization
{
Body of
i<=5 -> Condition
cout << i << " "; the loop ++I -> Uptation
}
}
Here is how program works
Syntax:
while(condition)
{
Body of the loop;
}
Example Program:
Display the numbers from 1 to 5.
#include<iostream.h>
void main()
{ Output
Output
int i = 1; 1
1223
3445
5
while (i <= 5)
{
cout << i << " ";
++i;
}
}
Do-while loop
A do…while loop is similar to while loop, except the fact
that it is body of the loop execute at least one time.
Syntax:
do
{
Body of the loop;
} while(condition)
Example Program:
Display the numbers from 1 to 5.
#include<iostream.h>
void main()
{ Output
Output
int i = 1; 1
1223
3445
5
do
{
cout << i << " ";
++i;
} while (i <= 5)
}
Differentiate between while and do…while loop
While Do…while
Entry control loop Exit control loop
The condition is first tested It execute the body once after it
True -> Block is executed check the condition
False -> Block is not executed True -> Block is executed
False -> Block is not executed