C if else Statement

The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.

There are many ways to use if statement in C language:

• If statement
• If-else statement
• If else-if ladder
• Nested if

If Statement

The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:

```
if(expression){
//code to be executed
}

```

Flowchart of if statement in C

Let's see a simple example of c language if statement.

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

```

Output

enter a number:4

4 is even number

enter a number:5

If-else Statement

The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:

```
if(expression){
//code to be executed if condition is true
}else{
//code to be executed if condition is false
}

```

Flowchart of if-else statement in C

Let's see the simple example of even and odd number using if-else statement in C language.

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

```

Output

enter a number:4

4 is even number

enter a number:5

5 is odd number

If else-if ladder Statement

The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:

```
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}

```

Flowchart of else-if ladder statement in C

The example of if-else-if statement in C language is given below.

```
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

```

Output

enter a number:4

number is not equal to 10, 50 or 100

enter a number:50

number is equal to 50

C - nested if statements

It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax

The syntax for a nested if statement is as follows

```
if( boolean_expression 1) {

/* Executes when the boolean expression 1 is true */
if(boolean_expression 2) {
/* Executes when the boolean expression 2 is true */
}
}

```

You can nest else if...else in the similar way as you have nested if statements.

Example

```
#include <stdio.h>

int main () {

/* local variable definition */
int a = 500;
int b = 700;

/* check the boolean condition */
if( a == 500 ) {

/* if condition is true then check the following */
if( b == 700 ) {
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}

printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );

return 0;
}

```

When the above code is compiled and executed, it produces the following result

Value of a is 500 and b is 700

Exact value of a is : 500

Exact value of b is : 700