Control Structures
Programs in C++ will be executed line by line from top
to bottom in an order.
By using control statements we can control the flow of
execution.
We have three types of control statements in C++:
1. Decision making Statements.
2. Iteration(looping) statements.
3. Branching statements
Decision making statements
Execute set of statements based on a condition
If
if-else
if-else ladder
Switch-case
Looping statements:
Execute a block of code several number of times. Then
looping statements are used
A loop statement allows us to execute a statement or group
of statements multiple.
while loop
do-while loop
for loop
Branching statements(loop control statements)
Loop control statements change execution from its
normal sequence.
It consists of
Break
Continue
Syntax:
switch(expression){
case 1 :
case1 statements;
break;
case 2:
case2 statements;
break;
default:
statements;
}
Simple if
Syntax:
if( condition )
{
// code
}
if-else
Syntax:
if( condition )
{
// code
}
else
{
//code
}
|
If-else ladder: | Switch-case:
Syntax: | Syntax:
if( condition ) switch(expression){
{
|
| case 1 :
// code
} | case1 statements;
else if | break;
{ | case 2:
//code case2 statements;
}
|
| break;
else
{ | default:
//code | statements;
} | }
while loop:
Repeats a statement or group of statements while a given condition is
true. It tests the condition before executing the loop body.
If the expression evaluates to true, the while statement executes
the statement(s) in the while block.
Syntax:
while (expression)
{
statements
}
do-while loop:
It is like a while statement, except that it tests the condition at
the end of the loop body.
Syntax:
do
{
statement(s)
}
while (expression);
for loop:
Here the statements repeatedly loops until a particular
condition is satisfied.
Syntax:
for (initialization; condition; increment)
{
statement(s)
}
Break:
When the break statement is encountered inside a loop,
the loop is immediately terminated and the program control
resumes at the next statement.
Continue:
The continue statement can be used in any of the loop control
structures.
The continue statement skips the current iteration.