Beruflich Dokumente
Kultur Dokumente
By:-AAKASH KAUSHIK
#9289817971, 98919893083
Email-theaakashkumar@gmail.com
UNIT -5
FLOW OF CONTROL
FLOW OF CONTROL
IT REFERS TO THE FLOW IN WHICH A
PROGRAM EXECUTION TAKES PLACE.
AAKASH KAUSHIK
9891983083,9289817971
FLOW OF CONTROL
1.)Sequential
IN this each statement of a program is
executed sequentially i.e, one after another
2.)Conditional/Decision Making
3.)Iterations/Looping
AAKASH KAUSHIK
9891983083,9289817971
CONDITIONS
TYPE OF CONDITIONS
IF
IF-ELSE
ELSE IF LADDER
NESTED IF
SWITCH
CONDITIONAL OPERATOR
AAKASH KAUSHIK
9891983083,9289817971
IF CONDITION
It takes a condition in parenthesis and a code block .If
the condition will results into true the code block will
execute or if the condition will false the code block will
be skipped.
The general form of a simple if statement is
if (test expression)
{
statement-block;
}
statement-x;
AAKASH KAUSHIK
9891983083,9289817971
IF ELSE CONDITION
It is the extension of simple if statement. It contains
an additional else block also. If the condition will
results into true the if code block will execute or if the
condition will false the else code block will execute.
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
AAKASH KAUSHIK
9891983083,9289817971
EXAMPLE
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<"Enter any number : ";
cin>>num;
if(num>0)
cout<<num<<" is Positive.";
else
cout<<num<<" is Negative.";
}
output
AAKASH KAUSHIK
9891983083,9289817971
EXAMPLE
#include<iostream.h>
#include<conio.h>
void main()
{
int amt,discount;
cout<<"Enter amount : ";
cin>>amt;
if(amt>20000)
discount=15;
else if(amt>15000)
discount=10;
else if(amt>10000)
discount=5;
else discount=0;
cout<<"You will get "<<discount<<"% discount.";
}
output
NESTED IF
AAKASH KAUSHIK
9891983083,9289817971
NESTED IF
if (test condition1)
If condition-1 will true
{
then its body will
if (test condition 2)
{
execute and
statement-1;
condition-2 is tested if
}
it will true statementelse
1 will execute
{
otherwise statement-2
statement-2;
will execute or if
}
condition-1 will false
}
then as a result
else
{
statement-3 will
statement-3;
execute and in all the
}
AAKASH KAUSHIK cases control will be
statement-x;
9891983083,9289817971
transferred to
EXAMPLE
output
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
cout<<"\nEnter value of A ,B,C ";
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
cout<<"\n\nA is Greatest";
else
cout<<"\n\nC is Greatest";
}
else
{
if(b>c)
cout<<"\n\nB is Greatest";
else
cout<<"\n\nC is Greatest";
}
AAKASH KAUSHIK
9891983083,9289817971
}
AAKASH KAUSHIK
9891983083,9289817971
SYNTAX:switch(expression)
{
case constant 1: statement 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;
break;
default : statement sequence n;
}
//default statement is optional
AAKASH KAUSHIK
9891983083,9289817971
Example of
switch
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int dow;
cout<<"Enter weekday number(1-7)";
cin>>dow;
switch(dow)
{
case 1:cout<<"MONDAY";
break;
case 2:cout<<"TUESDAY";
break;
case 3:cout<<"WEDNESDAY";
break;
case 4:cout<<"THURSDAY";
break;
case 5:cout<<"FRIDAY";
break;
case 6:cout<<"SATURDAY";
break;
case 7:cout<<"SUNDAY";
break;
default:cout<<"wrong number of day";
break;
}
getch();
AAKASH KAUSHIK
}
9891983083,9289817971
outpu
t
Usage
of
break
in
switch
The break statement used under switch is
one of C++ jump statements . whenever a
case constant is matched with expression
in the switch that particular part starts
execution and lasts until any break
statement is encountered or until the end
of switch. Due to break statements the
program execution jumps to line following
the switch i.e., outside the body of the
switch statement otherwise it will execute
all the statements following the matched
case in the switch.
AAKASH KAUSHIK
9891983083,9289817971
output
relational expressions
i.e., multiple
conditions.
If else is more
Ifversatile
else.. as it can
handle ranges.
If else can work upon
int , char & floating
point data also.
If else construction
lets you use a series
of expressions that
may involve unrelated
variables
AAKASH KAUSHIK
9891983083,9289817971
relational expressions
i.e., multiple
conditions.
If else is more
Ifversatile
else.. as it can
handle ranges.
If else can work upon
int , char & floating
point data also.
If else construction
lets you use a series
of expressions that
may involve unrelated
variables
AAKASH KAUSHIK
9891983083,9289817971
ITERATIONS/LOOPING
ITERATIONS/LOOPING
Same block/set of statements is
executed repeatedly (again &
again) until a specific condition is
fulfilled.
AAKASH KAUSHIK
9891983083,9289817971
PARTS OF LOOP
1.)Initialization Expression- It is the initialization
Statement of the Control variable( variable used
to control the loop) .i.e., The value from which
loop will start its execution
2.)Test Expression/Condition - the condition up to
which loop will keep on execution. If the
condition results true loop-body will execute
again otherwise the loop is terminated
3.)Updation- the loop variable is incremented or
decremented according to the needs for which
the next iteration will be performed.
4.)Body of loop- Code Block/Statements need to
be executed repeatedly until the conditions
AAKASH KAUSHIK
results true.
9891983083,9289817971
Type of LOOPS
FOR loop
WHILE loop
Do-WHILE loop
Nested loops
AAKASH KAUSHIK
9891983083,9289817971
FOR LOOP
Syntax:for(intializtion ; condition ; updation)
{
----BODY OF LOOP
--------}
AAKASH KAUSHIK
9891983083,9289817971
INTIALIZATION
CONDITION
CHECKING
FALSE
TERMINATE
LOOP
T
R
U
E
BODY
EXECUTION
UPDATION
AAKASH KAUSHIK
9891983083,9289817971
WHILE LOOP
Syntax:Initialization
While(condition)
{
------------}
BODY OF LOOP
+
UPDATION
AAKASH KAUSHIK
9891983083,9289817971
STAR
T
INTIALIZATION
CONDITION
CHECKING
FALSE
T
R
U
E
BODY
EXECUTION
+
UPDATION
AAKASH KAUSHIK
9891983083,9289817971
TERMINATE
LOOP
DO WHILE LOOP
Syntax:Initialization
do
{
------------}
While(condition)
BODY OF LOOP
+
UPDATION
AAKASH KAUSHIK
9891983083,9289817971
STAR
T
INTIALIZATION
BODY
EXECUTION
+
UPDATION
T
CONDITION
CHECKING
R U E
AAKASH KAUSHIK
9891983083,9289817971
THANK
YOU
AAKASH KAUSHIK
9891983083,9289817971