If else  statement 

In case of if else statement  if condition is true then if bock executed otherwise else block  or statement after the else block executed. Firstly let us see syntax of if else statement.

if (condition)
{
statement 1;
}
else
{
statement 2;
}
statement a;

Syntax of if else statement



In if else statement  if condition is true then all the statement in if block executed and if condition is false  then all then all the statement in  else block is  executed.
For example :-

Check whether number is odd or even.
1.)  #include<stdio.h>
int main()
{
int  x=10;
if(x%2==0)
{
printf("number is even");
}
else
{
printf("number is odd");
}
return 0;
}
Output -  number is even
Run this code

2.)  #include<stdio.h>
int main()
{

int  x=9;
if(x%2==0)
{
printf("number is even");
}
else
{
printf("number is odd");
}
return 0;
}
Output : number is odd
Run this code

In example 1  condition is true so if block executed but in case  example 2  condition is false so else block is executed.

If else if  (nested if else) statement

When if condition comes under the else block then statement is  known as nested if else. In if else if statement  second if condition becomes written under the else block. If else if work same as simple if else.

If(condition 1)
{
Statement ;
}
Else
{
If(condition 2)
{
Statement 2;
}
Else
{
Statement 3;
}
Statement x

Syntax of if else if statement .

If condition 1 is true then if block 1 executed and if false then else block 1 executed . under the else block 1 when if condition 2 is true then if block 2 executed and if false then else block 2 executed.
 Let us see an example.

#include<stdio.h>
int main()
{
int a,b;
printf("enter any two numbers:");
scanf("%d%d",&a,&b);
if(a==b)
printf(" both are equal numbers");
else
{
if(a>b)
printf("first number is greater than second");
else
printf("second number is greater than first");
}
return 0;
}
Output :-  
enter any two numbers: 6
7
second number is greater then first
Run this code

Notes :-
1. )Any number of statements can be written in if or else block.

Give your answer by following above rule and conditions of these question.

1.)  #include<stdio.h>
int main()
{
if(2==3)
printf("hello");
else
printf("suraj");
return 0;
}

2.)  #include<stdio.h>
int main()
{
int a=5,b=7;
if(a==7)
printf("numbers are equal");
else
{
if(a<b)
printf(" a is less than b");
else
printf(" b is less than a");
return 0;
}

<<Prev                              Next>>