Conditional Statement or
Decision Making Statements or Conditional Branching Statements
The statements are executed
based on the condition.
If the condition is true then the statements are executed.
If the condition is false then the statements are not executed.
C & C++ supports following types of conditional
statements.
- If
- If else
- If else if / else if /else if lader
- Nested if
1. ‘if’ Statement :
Syntax for ‘if ‘ statement:
If the condition is true, the control enters into the if
block and executes statements in the block .
If the condition is false, the control cannot enter into
the if block and does not executes statements in the block .
/*C++ program to check
eligibility for vote*/
Program
|
#include<iostream.h>
#include<conio.h>
Void main()
{
intvage;
clrscr(
);
cout<<“\nEnter
the age:”;
cin>>vage;
If(vage>=18)
{
cout<<“\nHe/She
is eligible for vote.”;
}
getch( );
}
Result:
Input:
Enter
the age: 23
Output:
He /
She is eligible for vote
|
2. ‘if else’ statement:
Syntax for ‘if else’ statement:-
If the condition is true,the control enters into the if
block and executes statements in the block .
If the condition is false,the control enters into else
block and executes statements in the block .
Note:-
For one if block there must be one else block .
No statements are allowed between if and else .
/*C++ program to check
eligibility for vote*/
Program
|
#include<iostream.h>
#include<conio.h>
Void main()
{
intvage;
clrscr(
);
cout<<“\nEnter
the age:”;
cin>>vage;
If(vage>=18)
{
cout<<“\nHe/She
is eligible for vote.”;
}
else
{
cout<<“\nHe/She
is not eligible for vote.”;
}
getch( );
}
Result:
Input:
Enter
the age: 13
Output:
He /
She is not eligible for vote
|
3. If else if / else if /
else if ladder Statement:
Syntax for ‘if else if’ statement:-
In this type of conditional statements the execution is
very fast because
If the 1st condition is true then it cannot
check the 2nd condition
If the 2nd condition is true then it cannot
check the 3rd condition
this process continues till the end of the block.
/*C++ program to find the greatest number of given
three numbers*/
Program
|
#include<iostream.h>
#include<conio.h>
Void main()
{
inta,b,c;
clrscr(
);
cout<<“\n
enter the values of a,b,c”;
cin>>a>>b>>c;
if(a>b&&a>c)
{
cout<<“\n”<<a<<” is greater”;
}
else
if(b>c)
{
cout<<“\n”<<b<<”is
greater”;
}
else
{
cout<<“\n
”<<c<<” is greater”;
}
getch( );
}
Result:
Input:
Enter
the values of a, b, c: 10
67
78
Output:
78 is
greater
|
4. “Nested if” Statement
:
Syntax for “nested if”:
If inside another if is known as nested if.
/*C++ program to display student result*/
Program
|
#include<iostream.h>
#include<conio.h>
Void main()
{
int sno,m1,m2,m3,tot;
float
avg;
clrscr(
);
cout<<"enter
sno,m1,m2,m3:";
cin>>sno>>m1>>m2>>m3;
tot=m1+m2+m3;
avg=tot/3;
cout<<"\nsno=“<<sno;
cout<<"\nm1=“<<m1;
cout<<"\nm2=“<<m2;
cout<<"\nm3=“<<m3;
cout<<"\ntotal=“<<tot;
cout<<"\navg=”<<avg;
if(m1>=35&&m2>=35&&m3>=35)
{
if(avg>=70)
{
cout<<"\distinction";
}
else if(avg>=60)
{
cout<<"\nfirst class";
}
else if(avg>=50)
{
cout<<"\second
class";
}
else if(avg>=40)
{
cout<<"\n third
class";
}
else if(avg>=35)
{
cout<<"\npass";
}
}
else
{
cout<<"\nfail";
}
getch( );
}
Result:
Input:
enter
sno,m1,m2,m3:1
96
85
42
Output:
sno=1
m1=96
m2=85
m3=42
total=223
avg=74.00
distinction
|
Switch statement:
Syntax:
If the expression value matches with any case const value
then the control executes that case statements.
If the expression value not matches with any case const
value then the control executes default statements.
Default can be placed any where in the switch block but
in general we place it at the end of block.
Switch statements expression values can take either int
or char but not take float and double.
Switch statements does not support duplication of case.
Break:-It is used to exit from the switch statement or
innermost of the loop.
/*C++ program to
demonstrate switch statement*/
Program
|
#include<iostream.h>
#include<conio.h>
Void main()
{
intrno;
clrscr(
);
cout<<“\nEnter
the student Roll. No:”;
cin>>rno;
switch(rno)
{
case 1:
cout<<“\nabc”;
break;
case
2:
cout<<“\nxyz”;
break;
case
3:
cout<<“\nmno”;
break;
case
4:
cout<<“\npqr”;
break;
default:
cout<<“\nInvalid
Roll num….try again!!”;
break;
}
getch( );
}
Result:
Input:
Enter
the student Roll. No: 3
Output:
mno
|
No comments:
Post a Comment