# if else - if, if else, nested and ladder

In this tutorial, We learn about different types of if else statements are simple if, if else, nested and ladder these all are decision making statement. We provide syntax for each, with examples.

Decision making contains much more are switch case, conditional operator and goto statement on this we learn in the next tutorial.

It is about dividing the program in the order of execution of certain statements based on some conditions. These are used when we have to set some limitations on statements based on conditions.

## C if statement

This statement is also known as simple if statement. Let us see the general form or syntax f the if statement in c programming then explain it.

```if( expression ) {
statement1;
statement2;
.
.
.
statementn;
}```

## How if statement work?

• The if statement starts the evaluation of the condition expression in the () parenthesis.
• If the expression evaluates result True, then it will execute the statements written inside the if body.
• If the expression evaluates result False, then it will execute the statements written outside the if body.

## Example for if statement

``````#include<stdio.h>
int main()
{
int n;
printf("\n Enter a number:");
scanf("%d",&n);
if(n==0)
{
printf("zero",n);
}
return 0;
}``````

## if else

Syntax of the if else statement in c programming then explain it.

```if( expression ) {
statement1;
statement2;
.
.
.
statementn;
}
else {
statement1;
statement2;
.
.
.
statementn;
}```

## Example for if else

``````#include<stdio.h>
int main()
{
int n;
printf("\n Enter a number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is an even number",n);
}
else
{
printf("%d is an odd number",n);
}
return 0;
}``````

## OUTPUT

``` Enter a number:5
5 is an odd number```

## nested if else

The general form or syntax of the nested if else statement in c programming is:

```if( expression 1 ) {
if( expression 2 ) {
statements;
}
else{
statements;
}
}
else {
if( expression 3 ) {
statements;
}
else{
statements;
}
}
```

## How nested if else statement work?

• The if statement starts the evaluation of the condition expression 1 in the () parenthesis.
• If the expression evaluates result True, then it will execute the if statement was written inside the if body. If, expression 2 is true then run statements within it, otherwise else part run of if with expression 2.
• If the expression evaluates result False, then it will execute the if statement was written inside the if body. If, expression 3 is true then run statements within it, otherwise else part run of if with expression 3.

## Example C nested if else

``````#include<stdio.h>
int main()
{
int year;
printf("\n Enter a year:");
scanf("%d",&year);
if(year%100==0)
{
if(year%400==0)
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap year",year);
}
}
else
{
if(year%4==0)
{
printf("%d is a leap year",year);
}
else
{
printf("%d is not a leap  year",year);
}
}
return 0;
}``````

## OUTPUT

```Enter a year:1900
1900 is not a leap year```

Syntax of the else if ladder statement in c programming then explain it.

```if ( expression1 ) {
statements;
}
else if( expression2 ) {
statements;
}
else if ( expression3 ) {
statements;
}
.
.
else {
statements;
}
```

## How else if ladder statement work?

• The if statement starts the evaluation of the condition expression 1 in the () parenthesis.
• The expression is tested from the top (of the ladder) downwards.
• As soon as a true condition is found, the statement associated with it is executed.

``````
#include<stdio.h>
int main()
{
int i,m1,m2,m3,m4,m5,rollno,tot;
float perc;
for(i=1;i<=5;i++)
{
printf("\n Enter the roll no:-");
scanf("%d",&rollno);
printf("\nenter marks in 5 subject");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
tot=m1+m2+m3+m4+m5;
perc=(float)(tot/5.0);
if(perc=90)
{
}
else
{
if(perc=80)
{
}
else
{
if(perc=70)
{
}
else
{
if(perc=50)
{
}
else
{
if(perc=36)
{
}
else
{
}
}
}
}
}
printf("\nrollno of student =%d",rollno);
printf("\nmarks in subject1=%d",m1);
printf("\nmarks in subject2=%d",m2);
printf("\nmarks in subject3=%d",m3);
printf("\nmarks in subject4=%d",m4);
printf("\nmarks in subject5=%d",m5);
printf("\ntotal marks in subject=%d",tot);
printf("\ntotal percentage in subject=%f",perc);
}
return 0;
}
``````

``````