Sie sind auf Seite 1von 33

C++ Language

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

THE ELSE IF LADDER

There is another way of putting ifs together


when multiple decisions are involved. A
multipath decision is a chain of ifs in which the
statement associated with each else is an if. It
takes the following general form:
if (condition 1)
statement 1 ;
else if (condition 2)
statement 2;
else if (condition n)
statement n;
else
default statement;
AAKASH KAUSHIK
statement x; 9891983083,9289817971

THE ELSE IF LADDER


If the condition-1 is true statement-1 will
be executed; otherwise it continues to
perform the other test condition-2 then
condition-3 and so on
Until any condition results into true
otherwise by default else block i.e, defaultstatement will get executed and control
will be transferred to statement X.
The else block is Optional and Default.
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

Nested if also known as if within if or


condition within condition. When a
series of decisions are involved, we
may have to use more than one
if...else statements in nested form
i.e., body of an if or else part will also
be a condition(if..else).

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
}

THE SWITCH STATEMENT


C++ Provides a multiple-branch selection
statement known as SWITCH. This selection
statement tests the value of an expression
against a list of integer or character constants.
When a match is found, the statement associated
with that constant are executed if no match is
found then the default statement gets executed.

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

Example of Usage of break in


switch
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<enter any num;
cin>>num;
switch(num)
{
case 1:cout<<One\n;
case 2:cout<<Two\n;
break;
case 3:cout<<Three\n;
case 4:cout<<Four\n;
break;
default:cout<<wrong entry;
}
getch();
}

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

Switch v/s if-else..


Switch can only test
for equality
Switch
Switch case label must
be only a single value.
Switch works only with
int & char data types.
The switch statement
selects its branches by
testing the value of
same variable(against
a set of constants)

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

Switch v/s if-else..


Switch can only test
for equality
Switch
Switch case label must
be only a single value.
Switch works only with
int & char data types.
The switch statement
selects its branches by
testing the value of
same variable(against
a set of constants)

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

FOR LOOP EXECUTION


SEQUENCE
STAR
T

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

WHILE LOOP EXECUTION SEQUENC

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

DO-WHILE LOOP EXECUTION SEQUENC


TERMINATE
LOOP
F
A
L
S
E

STAR
T

INTIALIZATION

BODY
EXECUTION
+
UPDATION
T

CONDITION
CHECKING

R U E

AAKASH KAUSHIK
9891983083,9289817971

THANK
YOU
AAKASH KAUSHIK
9891983083,9289817971

Das könnte Ihnen auch gefallen