Chapter : 3
Control flow
statements
- Simple if
- If-else
- Nested if-else
- Switch statement
- While loop
- Do-while loop
- For loop
- Nested loop
- Continue statement
- Break statement
Control flow statements
:
Sometime known
as control flow structures
In C++ there are two types of
control flow statements – i) conditional statements
ii) looping statements
Fig. bellow
shows the control flow statements in C++ language
Conditional
Statements :
Conditional
statements execute its body (true part or false part) one time if condition is
true or false.
C++ provide us three types of
conditional statements – i) if
ii) if…else
iii) switch
Simple if statement :
It is one of
the powerful conditional structure/statement provided by C++. In if statement
if condition is true then body of loop is executes otherwise not.
If the condition is true, then the
block of code inside the if statement will be executed. If condition is false,
then the set of code after the end of the if statement (after the closing curly
brace) will be executed.
Syntax :
if
( condition )
{
// BODY
OF IF
--------------------
--------------------
}
Example :
no = 20
if(
no % 2 == 0)
{
cout<<
no <<” is even ” ;
}
If….else statement :
In if….else statement, if condition
is true then body of if (true part) will be executed and if condition is false
then body of else (false part) will be executed.
An if statement can be
followed by an optional else statement, which executes when the
condition is false
If the condition evaluates to true,
then the if block of code will be executed, otherwise else block of
code will be executed.
Syntax :
if ( condition )
{
// BODY
OF IF
--------------------
--------------------
}
else
{
// BODY
OF ELSE
--------------------
--------------------
}
Example :
a = 10; b
= 20;
if(
a > b )
{
cout<<”a
is large”;
}
else
{
cout<<”b
is large”;
}
Nested if…else statement : (OR if….else if….else statement)
If we use if….else statement inside
another if….else statement (inside else part) then it is known as nested
if…..else statement. (That is if….else within another if…..else statement.)
When we using nested if…else
statements there is one thing we have to understand : An if can have zero or one
else statement and it must come after if statement (body of if).
Syntax :
if ( condition )
{
// BODY
OF IF
--------------------
--------------------
}
else
{
// BODY
OF ELSE
if
( condition )
{
// BODY
OF IF
--------------------
--------------------
}
else
{
// BODY
OF ELSE
--------------------
--------------------
}
--------------------
--------------------
}
Example :
int age = 23;
if(age < 110
&& age > 60)
{
cout<<”
Old Person”;
}
else
{
if(age > 30)
{
cout<<”
Elder Person”;
}
else
{
if(age
> 15)
{
cout<<”
Young ”;
}
else
{
cout<<”
Child”;
}
}
}
Switch statement (Branching statement) :
Switch statement is also known as
branching statement.
In switch statement, it search for
matching/equal caonstant_expression in cases, if case is match/equal then
statements within case will be executed.
If expression doesn’t match with any
case then default part (default case) will be executed
A switch statement allows a variable
to be tested for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each case.
Syntax :
Switch(expression)
{
Case
constant_expression_1 :
Statements;
Break;
Case
constant_expression_2 :
Statements;
Break;
.
.
.
.
Case
constant_expression_n :
Statements;
Break;
Default :
Statements;
}
Example : sample
code to print roman equivalent of 1 to 5 numbers.
no = 3;
switch ( no )
{
Case 1 :
Cout<<
” I ”;
Break;
Case 2 :
Cout<<
” II ”;
Break;
Case 3 :
Cout<<
” III ”;
Break;
Case 4 :
Cout<<
” IV ”;
Break;
Case 5 :
Cout<<
” V ”;
Break;
Default :
Cout<<”
Please enter number within 1 to 5”;
}
Looping Statements :
Sometime in programming we need to
execute same code several times (again and again), so in place of writing same
code again and again we use loops. C++ provide us three types of looping
structures are as follows – i) while
loop
ii)
do….while loop
iii)
for loop
A loop statement
allows us to execute a statement or group of statements multiple times.
While loop :
While loop repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the loop body that’s
why it known as Entry Control Loop
(Entry Control Loop : When condition is
checked before enter into body of loop)
A while loop
statement repeatedly executes a target statement as long as a given condition
is true.
Syntax :
while ( condition )
{
// BODY
OF WHILE
--------------------
--------------------
}
Example : following
is the sample code to print 1 to 10 numbers.
int i = 1;
while (
i <=
10 )
{
cout<<
” \t ” << i ;
i++
;
}
Output :
1 2 3 4 5 6 7 8 9 10
Do-While loop :
Do
while loop is exit control loop
(Condition
will be check after execution body of loop i.e. at exit time)
·
Unlike for and
while loops, which test the loop condition at the beginning (top) of the loop,
the do...while loop checks its condition at the end (bottom) of the loop.
·
A do...while loop
is similar to a while loop, except that a do...while loop is guaranteed to
execute body of loop at least one time.
·
No matter whether
condition is true or false, do….while loop executes its body (body of loop) at
least one time
Syntax :
do
{
// BODY
OF LOOP
--------------------
--------------------
} while (
condition );
Example : sample
code to print IMRD 5 times
int i =
1 ;
do
{
cout<<” I.M.R.D.
”;
i++;
} while (i <=
5 );
Output :
I.M.R.D. I.M.R.D. I.M.R.D. I.M.R.D. I.M.R.D.
Difference Between while loop and do-while loop :
For loop :
for loop repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body that’s why it
known as Entry Control Loop
(Entry Control Loop : When condition is
checked before enter into body of loop)
Using for loop
we can declare and initialize variable, check condition, and increment or
decrement counter on the same line (inside for(…) ).
A for loop
statement repeatedly executes a target statement as long as a given condition
is true.
Syntax :
for ( initialization ; condition ; increment / decrement )
{
// BODY
OF LOOP
--------------------
--------------------
}
Example : sample
code to print some of 1 to 10 numbers
for ( int i
=10 ; i>=1 ; i-- )
{
cout<<”\t”<<I;
}
Output :
10 9 8 7 6 5 4 3 2 1
Nested Loops (Or Nesting of Loop) :
A loop can be nested inside of
another loop.
C++ allow us to use one loop within
another loop is known as nested loops or nesting of loop.
General syntax :
Outer_loop(condition)
{
Inner_loop_1(condition)
{
Inner_loop_2(condition)
{
-
- - - - - - - - - - - -
-
- - - - - - - - - - - -
Inner_loop_n(condition)
{
-
- - - - - - - - - - - -
-
- - - - - - - - - - - -
} // Inner_loop_n close
} // Inner_loop_1 close
} // Inner_loop_2 close
} // Outer_loop close
·
Nested
while loop :
Syntax :
while(condition)
{
while(condition)
{
statements;
}
statements; // you
can put more statements.
}
·
Nested
do…while loop :
Syntax :
do
{
statement(s);
// you can put more statements.
do
{
statement(s);
}while(
condition );
}while( condition );
·
Nested for loop :
Syntax :
for ( initialization;
condition; increment/decrements )
{
for (initialization; condition; increment/decrements)
{
statements;
}
statement(s);
// you can put more statements.
}
Jumps in
loop :
To
change execution of loop from its normal
execution C++ provide us some statements which controls loop execution.
C++ supports the following control
statements.
i.
Break statement
ii.
Continue statement
iii.
Go to statement
Break Statement :
The break
statement has the following two usages in C++:
When the break statement is
encountered inside a loop, the loop is immediately terminated and program
control resumes at the next statement following the loop.
It can be used
to terminate a case in the switch statement (covered in the next chapter).
If you are using nested loops (i.e.,
one loop inside another loop), the break statement will stop the execution of
the innermost loop and start executing the next line of code after the block.
Syntax :
break;
Example :
int a = 1;
do
{
if( a == 5)
{
// break
the execution of loop
a = a +
1;
break;
}
cout
<< "\t value of a: " << a;
a = a + 1;
}while( a <= 7 );
Output :
value of a: 1
value of a: 2 value of a: 3 value of a: 4
Continue Statement :
The continue statement
works somewhat like the break statement. Instead of forcing termination,
however, continue forces the next iteration of the loop to take place, skipping
any code in between.
Syntax :
continue;
Example :
int a = 1;
do
{
if( a == 5)
{
// skip
the iteration.
a = a +
1;
continue;
}
cout
<< "\t value of a: " << a;
a = a + 1;
}while( a <= 7 );
Output :
value of a: 1
value of a: 2 value of a: 3 value of a: 4 value
of a:6 value of a:7