Conditional Statements in C++
If Statement
If Statement is used to check the Condition. If the Condition is True then It will enter into the if block and perform the operations. If the Condition is Flase then it will terminate the Program.Syntax
if (condition)
{
Statements;
}
Flow Chart
If Else Statement
In this If the Condition is True then It will enter into the if block and perform the operations. If the Condition is False then it will enter into else block and execute the Statements.Syntax
if (condition)
{
Statements;
}
else
{
Statements;
}
Flow Chart
If Else If Statement
It is used when we have multiple Conditions. In this If the Condition is True then It will enter into the if block and perform the operations. If the Condition is False then it will check another condition and so on.Syntax
if (condition)
{
Statements;
}
else if(condition)
{
Statements;
}
.
.
.
else
{
Statements;
}
Flow Chart
Nested If Statement
It is used Check conditions step wise . In this If the Condition is True then It will enter into the if block and then it will check another condition and executes the statements. If the Condition is False will enter into else block and executes the statements.And this goes so on.Syntax
if (condition)
{
if(condition)
{
Statements;
}
else
{
Statements;
}
}
else
{
Statements;
}
Flow Chart
Switch Statements
It is used for multiway banching statements. In this the condition is matched based on cases.Syntax
switch(choice)
{
case Number/'Character':
Statements;
break;
.
.
.
.
case N:
Statements;
break;
default:
Statements;
}
Programs
If Condition
WAP to check the entered number is Even?
If-else Condition
WAP to check the entered number is Even or Odd?
If-else-if Condition
Write a program to evaluate the Grade of a student for the following constraints:
If marks > 75 – grade A
If 60 < marks < 75 – grade B
If 45 < marks<60 – grade C
If 35 < marks<45 - grade D
If marks < 35 – grade E
If marks > 75 – grade A
If 60 < marks < 75 – grade B
If 45 < marks<60 – grade C
If 35 < marks<45 - grade D
If marks < 35 – grade E
Nested If Condition
WAP to check the entered number is Even or Odd and Number Should be Greater than 10?
Comments
Post a Comment
If you have any doubts, Please let me Know