0% found this document useful (0 votes)
6 views22 pages

Decision Making & Branching

decision maikng

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

Decision Making & Branching

decision maikng

Uploaded by

iamavp1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Decision Making &

Branching
Decision Control Statements
• In decision control statements (C if else and
nested if), group of statements are executed
when condition is true. If condition is false,
then else part statements are executed.

• There are 4 types of decision making control


statements in C language. They are,
• if statements
• if else statements
• nested if else statements

if statement
• In these type of statements, if
condition is true, then respective block
of code is executed.

Syntax
if(condition)
{
Statements;
}
Flow Chart – if statement

4
Example – if statement
void main()
{
int m=40,n=40;
if (m == n)
{
cout<<"m and n are equal”;
}
}

Output
m and n are equal
5
if-else statement
• In these type of statements, group of statements are
executed when condition is true. If condition is false, then
else part statements are executed.

Syntax
if(condition)
{
Statement1;
Statement2;
}
else
{
Statement3;
Statement4; 6
Flow Chart – if-else statement

7
Example – if-else statement
#include <iostream.h>
void main() Output
{
m and n are not equal
int m=40,n=20;
if (m == n)
{
cout<<"m and n are equal”;
}
else
{
cout <<"m and n are not equal”;
}
}
8
nested if else
• It is a conditional statement which is used when we want to check
more than 1 conditions at a time in a same program.

• When a series of decisions are involved, we may have to use more


than one if....else statement in nested form as shown below,

9
Flow Chart – nested if-else statement

10
Example – nested if-else statement
#include <iostream.h>
void main()
{
int m=40,n=20; Output
if (m>n)
{
m is greater than n
cout<<"m is greater than n";
}
else if(m<n)
{
cout<<"m is less than n”;
}
else
{
cout<<"m is equal to n“;
}
}
11
else if ladder
• The conditions are evaluated from the top
of the ladder downwards. As soon as a
true condition is found, the statement
associated with it is executed skipping the
rest of the ladder. The following
construct is known as the else if ladder.

12
Syntax

if(test condition)
{

}
else if(condition)
{

}
else if(condition)
{

}
else
{
13
Flow Chart –else if ladder statement

14
Example –if-else ladder statement
#include<iostream.h>
#include<conio.h>
void main( )
{
float a,b,res;
char op;
clrscr( );
cout<<"Enter two Numbers : “;
cin>>a,b;
cout<<"Enter the Operator : “;
cin>>op;
if(op=='+')
res=a+b;
else if(op=='-')
res=a-b;
else if(op=='*')
res=a*b;
else if(op=='/')
res=a/b;
else
{
cout<<"Invalid Operator ! \n Press any key to exit...";
getch( );
exit(0);
}
cout<<a,op,b,res ;
getch( );
} 15
Case Control statements
• The statements which are used to execute
only specific block of statements in a given
series of block are called case control
statements.

• There are 4 types of case statements in C


language. They are,
• switch case
• break
• continue
16
switch case statement
• This is used to execute only specific case statements
based on the switch expression.
Syntax
switch (expression)
{
case label1:
statements;
break;
case label2:
statements;
break;
default:
statements;
break; 17
Example
#include<iostream.h>
void main()
{
int day;
cout<<"enter any day as number”;
cin>>day;
switch(day)
{
case 0 : cout<<"the day of week is Sunday”;
break;
case 1 : cout<< "the day of week is Monday”;
break;
case 2 : cout<< "the day of week is Tuesday” ;
break;
case 3 : cout<<“the day of week is wednwesday:”;
break;
case 4 : cout<<"the day of week is Thursday:”;
break;
case 5 : cout<<"the day of week is Friday:”;
break;
case 6 : cout<<"the day of week is Saturday:”;
break;
default:cout<<“ the day does not exist:”;}
}
getch(); 18
break statement
• Break statement is used to terminate the
while loops, switch case loops and for
loops from the subsequent execution.

Syntax
break;

19
Example
#include <stdio.h>
void main()
{ Output
int i;

for(i=0;i<10;i++) 0
{ 1
if(i==5) 2
{ 3
cout<<"Coming out of for loop
4
when i = 5”;
break; Coming out of for loop
} when i = 5
cout<<,i;
}
}
20
continue statement
• Continue statement is used to continue the
next iteration of for loop, while loop and do-
while loops. So, the remaining statements
are skipped within the loop for that
particular iteration.

Syntax
continue;

21
goto statement
• goto statements is used to transfer the normal flow
of a program to the specified label in the program.

Syntax
{
…….
go to label;
…….
…….
label:
Statements;
} 22

You might also like