0% found this document useful (0 votes)
13 views

Control Structures in C++

The document discusses different control structures in C++ programming including sequence structure, selection structure (if statement, if-else statement, nested if-else statement, if-else ladder), and iteration structure (for loop, while loop, do-while loop). It provides syntax and examples for each structure. Selection structure allows different processes based on condition being true or false. Iteration structure includes initializing a condition variable, testing the control statement, executing the body of the loop, and updating the condition variable.

Uploaded by

Raj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Control Structures in C++

The document discusses different control structures in C++ programming including sequence structure, selection structure (if statement, if-else statement, nested if-else statement, if-else ladder), and iteration structure (for loop, while loop, do-while loop). It provides syntax and examples for each structure. Selection structure allows different processes based on condition being true or false. Iteration structure includes initializing a condition variable, testing the control statement, executing the body of the loop, and updating the condition variable.

Uploaded by

Raj Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Handled by

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

Types While 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

Iteration Variable i <= 5 Action

1st i=1 true 1 is printed. i is increased to 2.

2nd i=2 true 2 is printed. i is increased to 3.

3rd i=3 true 3 is printed. i is increased to 4.

4th i=4 true 4 is printed. i is increased to 5.

5th i=5 true 5 is printed. i is increased to 6.

6th i=6 false The loop is terminated


While loop
It is a repetitive control structure. It is used to execute the
set of statements within the body until the condition become false.

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

Loop will not be executed if the Loop will be executed at least


condition is false. once even the condition is false.
THANK YOU

You might also like