Vous êtes sur la page 1sur 14

Statement and Flow Control

Statements
Statements are the instructions given to the computer to perform any kind of action. A
statement is a smallest executable unit within a C++ program. Statements are always
terminated by semicolon.
Compound (Block) Statement
A compound statement is a grouping of statements in which each individual statement
ends with a semi-colon. The group of statements is called block. Compound statements
are enclosed between the pair of braces ({}.). The opening brace ({) signifies the
beginning and closing brace (}) signifies the end of the block.
Null Statement
Writing only a semicolon indicates a null statement. Thus ';' is a null or empty
statement. This is quite useful when the syntax of the language needs to specify a
statement but the logic of the program does not need any statement. This statement is
generally used in for and while looping statements.

Conditional (Branching) Statements


Sometimes the program needs to be executed depending upon a particular condition.
C++ provides the following statements for implementing the decision making structure.
if statement
if else statement
nested if statement
switch statement
if statement
syntax:
if (condition)
{
statement(s);
}
If the condition evaluates to true, then the block of code inside the if statement will be
executed. If condition evaluates to false, then the first set of code after the end of the if
statement (after the closing curly brace) will be executed.
Example
#include <iostream.h>
void main ()
{

1
int a = 10;
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}

Flow Diagram:

if statement if-else statement

if else statement
syntax
if (condition)
{
statements;
}
else
{
statements;
}

2
If the boolean expression evaluates to true, then the if block of code will be executed,
otherwise else block of code will be executed.
Example
#include <iostream.h>
int main ()
{
int a = 100;
if( a < 20 )
{
// if condition is true then print the following
cout << "a is less than 20;" << endl;
}
else
{
// if condition is false then print the following
cout << "a is not less than 20;" << endl;
}
cout << "value of a is : " << a << endl;
return 0;
}

Nested if statement
It is always legal to nest if-else statements, which means you can use one if or else if
statement inside another if or else if statement(s).

Syntax:
The syntax for a nested if statement is as follows:
if( condition 1)
{
// Executes when the condition1 is true
if(condition2)
{
// Executes when the condition2 is true
}
}
You can nest else if...else in the similar way as you have nested if statement.

3
Example
#include <iostream.h>
int main ()
{
int a = 100, b = 200;
if( a == 100 )
if( b == 200 )
cout << "Value of a is 100 and b is 200" << endl;
cout << "Exact value of a is : " << a << endl;
cout << "Exact value of b is : " << b << endl;
return 0;
}

if-else-if example
if(percentage>=60)
cout<<"Ist division";
else if(percentage>=50)
cout<<"IInd division";
else if(percentage>=40)
cout<<"IIIrd division";
else
cout<<"Fail" ;

switch statement
The if and if-else statements permit two way branching whereas switch statement
permits multiple branching. The syntax of switch statement is:
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
// you can have any number of case statements.
default : //Optional
statement(s);
}
4
The execution of switch statement begins with the evaluation of expression. If the value
of expression matches with a case then the statements following that case will execute
until a break statement is reached. The break statement transfers control to the end of
the switch statement. If the value of expression does not match with any constant, the
statement with default is executed.
Some important points about switch statement
The expression of switch statement must be of type integer or character type.
The default case need not to be used at last case. It can be placed at any place.
The case values need not to be in specific order.
Flow Diagram:

Example:
#include <iostream.h>
int main()
{
char grade = 'D';
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
5
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}

Looping statements
Sometimes we require a set of statements to be executed a number of times by
changing the value of one or more variables each time to obtain a different result. A loop
statement allows us to execute a statement or group of statements multiple times. C++
provides the following construct
while loop
do-while loop
for loop
for loop
It is used when you know in advance how many times the loop is to be executed. Syntax
of for loop
for (initialization; decision; increment/decrement)
{
statement(s);
}

Here is the flow of control in a for loop:

The initialization step is executed first, and only once. This step allows you to
declare and initialize any loop control variables.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If
it is false, the body of the loop does not execute and flow of control jumps to the
next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to the
increment/decrement statement. This statement allows you to update any loop
control variables.

6
The condition is now evaluated again. If it is true, the loop executes and the
process repeats itself (body of loop, then increment/decrement step, and then
again condition). After the condition becomes false, the for loop terminates.
Flow Diagram

Example
#include <iostream.h>
int main ()
{
// for loop execution
for( int a = 10; a < 20; a = a + 1 )
{
cout << "value of a: " << a << endl;
}
return 0;
}

While loop
It repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body. Syntax of while loop
while(condition)
{
statement(s);
}

7
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value. The loop iterates
while the condition is true. When the condition becomes false, program control passes to
the line immediately following the loop.
Flow Diagram:

Example:
#include <iostream.h>
void main ()
{
int a = 10;
while( a < 20 )
{
cout << "value of a: " << a << endl;
a++;
}
}

do...while loop
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to
execute at least one time. The syntax of a do...while loop in C++ is:

8
do
{
statement(s);
}while( condition );
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in
the loop execute again. This process repeats until the given condition becomes false.
Flow Diagram:

Example
#include <iostream.h>
int main ()
{
int a = 10;
// do loop execution
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
9
}

Nested loops
A loop can be nested inside of another loop.
Syntax
The syntax for a nested for loop statement in C++ is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested while loop statement in C++ is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s); // you can put more statements.
}
The syntax for a nested do...while loop statement in C++ is as follows:
do
{
statement(s); // you can put more statements.
do
{
statement(s);
}while( condition );
}while( condition );

Example
The following program uses a nested for loop to find the prime numbers from 2 to 100:

10
#include <iostream.h>
void main ()
{
int i, j;
for(i=2; i<100; i++) {
for(j=2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) cout << i << " is prime\n";
}
}

Jump Statements
A C++ jump statement performs an immediate transfer of control. C++ supports the
following control statements.
The break statement
It terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch. The break statement has the following two
usages in C++:
If you are using nested loops, the break statement will stop the execution of the
innermost loop and start executing the next line of code after the block.
Syntax
The syntax of a break statement in C++ is:
break;

Flow Diagram

11
Example
#include <iostream.h>
void main ()
{
int a = 10;
do
{
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15)
break;
}while( a < 20 );
}

The continue statement


The continue statement terminates current iteration and forces the next iteration of the
loop to take place, skipping any code in between.
For the for loop, continue causes the conditional test and increment portions of the loop
to execute. For the while and do...while loops, program control passes to the
conditional tests.

Syntax:
The syntax of a continue statement in C++ is:
continue;

Flow Diagram:

12
Example:
#include <iostream>
using namespace std;
void main ()
{
int a = 10;
do
{
if( a == 15)
{
// skip the iteration.
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
}

The goto statement


A goto statement provides an unconditional jump from the goto to a labeled statement
in the same function.
NOTE: Use of goto statement is highly discouraged because it makes difficult to trace
the control flow of a program, making the program hard to understand and hard to
modify. Any program that uses a goto can be rewritten so that it doesn't need the goto.
Syntax:
The syntax of a goto statement in C++ is:
goto label;
..
.
label: statement;
Where label is an identifier that identifies a labeled statement. A labeled statement is
any statement that is preceded by an identifier followed by a colon (:).

13
Flow Diagram

Example
#include <iostream>
using namespace std;
void main ()
{
int a = 10;
LOOP:do
{
if( a == 15)
{
a = a + 1;
goto LOOP;
}
cout << "value of a: " << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}

14

Vous aimerez peut-être aussi