The if-else statement is a two-way decision-making statement. it is used to
control the flow of execution of the statements and also used to carry out the
logical test and then pick up one of the two possible. usually if-else
elements in checking the condition.
Then. The syntax for the if-else statement is
if(condition)
{
Statement 1;
}
else
{
Statement 2;
}
flow chart:-
this the flow chart for the if-else condition.
program:
|
#include<stdio.h>
#include<conio.h> void
main () {
int
a=10,b=5;
if(10>5)
{
printf("easy to learn free coding");
}
else{
printf(“nothing to see");
}
getch();
}
|
output:
|
output:
|
|
easy to learn free coding
|
In this program, We have used if condition (10>5). Our condition is true
then it will print our given statement. Suppose if our condition is false it
will print the else below given statement. This the if-else element
condition.
nested if-else statement:
The if-else nested if the element is a multi-way decision-making statement,
It is used to control the flow of execution of the statements when there are
more than three alternatives. If the conditional is true then statements 1
are executed. If the conditional is false then the check for a second and
goes till it finds a suitable matching condition. If there is no matching
with any of the if conditions finally else blocks (default block) will be
executed.
then, the syntax for the nested if the condition
if(condition 1)
{ statement 1;
}
else if(condition 2)
{
statement 2;
}
else if(condition 3)
{
statement 3;
}
.
.
.
.
else {default statement;
>
}
flow chart:
this the flow chart for nested if condition.
program:
|
#include<stdio.h>
#include<conio.h> void
main() {
int
a=5, b=5;
clrscr();
if ((a>1 )&&(b >1))
{
if(a==b){
printf
("both values are same");
}
else{
printf("both values are not same");
}
}
else{ printf ("the conditions has not true");
}
getch ();
}
|
output:
|
output:
|
|
both values are same.
|
Then, the above the program
We are first to check our condition . I have given a>1 and
b >1. This condition is correct so it will come nested if
statement, nested if statement again I have given
one another, a condition which is a==b, then this condition
also true, so it will be print
both values are same
No comments:
Post a Comment