Professional Documents
Culture Documents
-Made by
SHRABANTI
FLOW OF CONTROL
INTRODUCTION
Every C++ program must have at least one function called main().
When we run a C++ program, the first statement executed will be
at the beginning of main() and the last statement at the end of
main(). Therefore, the main() is also known as driver function as
it drives the program.
The Von-Neumann architecture of computers supports only sequential
processing. The normal flow of execution of statements in a high-level
language program is also in the sequential order, that is, each
statement is
executed in its order of occurrence in the program. Some problems
often require
That the normal flow of control be altered depending on the
requirements.
C++ supports a number of control structures to perform the processing.
CONTROL STRUCTURES
Control Structures
Selection
if-else
switch
Sequence
Loop or Iteration
while , for
do-while
STATEMENTS
These are the instructions given to the computer to perform some
action and form the smallest executable unit within a C++ program.
A semicolon (;) terminates a statement.
SIMPLE STATEMENT: It is a single statement.
COMPOUND STATEMENT: It is a group of statements separated from each other
by a
semicolon(;). This group of statements is also called a
block
of code enclosed within a pair of curly braces { }. The
significance of the block is that the group of
statements
enclosed within the curly braces is treated as a single
unit.
If-statement
It tests a condition. The statements associated with if is (are)
executed
only when the condition is true, otherwise the statements is
(are) not
executedif(expression)
at all. The syntax of if statement is as shown below:
{
statement;
}
If-else statement
It tests a condition. Statement 1 is executed when the condition is
true otherwise
if(expression)
Statement 2 is executed. The syntax of if-else statement
is:
{
statement;
}
else
{
statement;
}
where if is the
keyword,
expression is the
booleon
expression, else is
the keyword and
statement can
be simple or
Nested if
One or more if statements embedded within the if statement
are called nested ifs.
The following if-else statement is a nested if statement nested
to level two:
//to print whether the entered no. is positive, negative or equal to zero
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<Enter the number;
cin>>num;
if(num==0)
cout<<Number is equal to zero;
else
{
if(num>0)
cout<<Number is positive;
else
cout<<Number is negative;
}
getch();
}
switch
This statement is used when we have to select one option out of
many alternatives.
It is a multi branch statement that makes the control to jump to
one of the several
switch(expression)
statements based on{ the value of an integer variable or an
expression. The general
case constant 1: statement
Form of the switch is:
sequence 1;
break;
case constant 2: statement
sequence 2;
break;
case constant 3: statement
sequence 3;
break;
.
.
.
case constant n-1: statement
sequence n-1;
REPETITIVE EXECUTION
Repetitive Execution means a set of statements are executed
again and again
till the condition remains true and if the condition is false, the
statement next
to those set of statements would be executed. There are3
types of statements
to implement repetative execution (Iteration/Looping).
while statement (Entry controlled loop-Pretest)
do while statement (Exit controlled loop-Post test)
for statement (counter controlled loop or deterministic loop)
while loop
It may not be executed even once if the condition is false
initially. It is executed
till the condition remains true and the control comes out of the
loop when the
condition becomes false. There must be some loop
terminating condition inside the
body of the loop to avoid infinite looping.
while(expression)
The syntax
{ of while statement is as follows:
statement;
}
where, while is the keyword, expression
is the booleon expression which evaluates
either to true or false & statement can be
simple or compound statement.
do-while loop
This loop is executed at least once. It is executed till the
condition remains true
and the control comes out of the loop when condition
becomes false. There must
Be some loop terminating condition inside the body of the loop
to avoidinfinite
Looping.
do
{ of do-while loop is as follows:
The syntax
statement;
}
while(expression)
;
where, while is the keyword, expression
is the booleon expression which evaluates
either to true or false & statement can be
simple or compound statement.
for loop
The loop controlled variable is assigned some value.
The condition is checked.
If the condition is true, then the body of for loop is executed.
Then, the arithmetic expression is evaluated.
After the evaluation the condition is checked again and if it
is true, then
again the body of for loop is executed and then the
arithmetic expression
is checked again.
The for loop is executed again and again till the condition
remains true.
for(initial
assignment;condition;arithmetic
expression)
The general
syntax of
the for loop is:
{
Nested loop
Nesting of loops means one or more loops within
a loop. In this case
inner loop must terminate before the outer loop.
//to print number pattern
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=20;++i)
{
for(j=1; j<=20-i;++j)
cout<< ;
for(j=1;j<=i;++j)
cout<<* ;
}
cout<<endl;
getch();
}
Output screen
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * *
JUMP STATEMENTS
break statement
: The break statements enable a program to skip ove