If else statements works on a boolean value. If the boolean value is true then the statements under if are executed else they are skipped. In C any non-zero integer value is treated as true and zero is treated as false.
Basically there are four types of if-else:
- Simple If
- If-Else
- If-Else-If (Ladder if)
- Nested if
Simple if:
As the name specifies Simple If is the simplest version of If. They contain single condition expression needs to be checked and if the condition is satisfied(true) one or more statements are executed. They do not contain any statement for else part(Part if the condition fails).Syntax
if (condition)
{
statement/s that will execute if the condition is true.
}
Flow chart of Simple if statement |
Lets see a simple program to understand:
Write a C program to input any number and print Hello, World! if the number is greater than 1.
#include <stdio.h> int main() { int n; printf("Enter any number: "); scanf("%d", &n); if( n>1 ) //Checks the condition that n > 1 or not. { printf("Hello, World!"); } return 0; }Output 1:
Enter any number: 10
Hello, World!
Output 2:Hello, World!
Enter any number: 0
In the output 1 users enters 10, that is greater than 1 hence the if condition will execute likeif ( 10>1 ) which is true
Since the if condition is true body of if will execute and prints "Hello, World!".
In output 2 user enters 0, that is less than 1 hence the if condition will execute like
if ( 0>1 ) which is false
Since the if condition is false the body of if is skipped printing nothing.
Note: You can also write if else without curly brace if there is only one statement under if else.
Example:
Lets consider another example
Lets see what happens when we remove curly brace{ }
Hence you should be careful while using curly braces { }.
Using curly brace { } with if else is also consider as a Good programming practice.
Example:
#include <stdio.h> int main() { int n = 5; if(n>0) { printf("This is positive number"); } return 0; }Above program can also be written as
#include <stdio.h> int main() { int n = 5; if(n>0) printf("This is positive number"); return 0; }Since it contains only one statement under if-else hence curly brace { } can be removed.
Lets consider another example
#include <stdio.h> int main() { int n = 5; if(n>0) { printf("This is positive number"); printf("More things to be done"); } return 0; }In above program both printf() will be executed only and only if n>0
Lets see what happens when we remove curly brace{ }
#include <stdio.h> int main() { int n = 5; if(n>0) printf("This is positive number"); printf("More things to be done"); return 0; }When considering the above program the first printf() will be executed only and only if n>0 But the second printf() will always execute whether the if condition is true or false, as it is not the part of if statement.
Hence you should be careful while using curly braces { }.
Using curly brace { } with if else is also consider as a Good programming practice.
If-else:
If else statement are the enhanced version of simple if. In simple if one or more statement/s are executed if the condition is true but doesn't describes any statement/s, if the condition is false. If-else describes statements for both whether the condition is true or false. In both case one or more statement/s are executed.If the condition is true body of if is executed skipping the body of else.
If the condition is false body of else is executed skipping the body of if.
Syntax of if else if (condition)
{
statement/s that will execute if the condition is true.
}
else
{
statement/s that will execute if the condition is false.
}
Flow chart of if-else statement |
Here is a sample program of if else:
Write a program to input any number and check whether it is negative or positive.
#include <stdio.h> int main() { int n; printf("Enter any number: "); scanf("%d", &n); if( n<0 ) //If any number is less than 0 it is negative { printf("Negative"); } else //If any number is not less than 0 it is not negative. { printf("Positive"); } return 0; }Output 1:
Enter any number: -5
Negative
Output 2:
Negative
Enter any number: 6
Positive
In output 1, when user enters -5 that is less than 0. The if statement will execute like
Positive
if( -5 < 0 ) which is true executing the body of if and skipping the body of else. Hence will print Negative.
In output 2, when user enters 6 that is greater than 0. The if statement will execute like
if( 6 < 0 ) which is false executing the body of else and skipping the body of if. Hence will print Positive.
If-else-if (Ladder if):
If-else-if statements are also called as ladder if due to their control flow. They are generally the enhanced version of if-else.Syntax of ladder if if (condition1)
{
statement/s that will execute if condition 1 is true.
}
else if (condition2)
{
statement/s that will execute if the condition 2 is true.
}
else if (condition3)
{
statement/s that will execute if the condition 3 is true.
}
...
...
...
else if (condition n)
{
statement/s that will execute if the condition n is true.
}
else
{
statement/s that will execute if all conditions are flase.
}
Flow chart of if-else-if (ladder if) |
Simple example of ladder if
Wite a program to input any number between 1-7 and print equivalent day name. For example: 1-Monday, 2-Tuesday etc.
#include <stdio.h> int main() { int n; printf("Enter day number between 1-7: "); scanf("%d", &n); if( n == 1) { printf("Monday"); } else if(n==2) { printf("Tuesday"); } else if(n==3) { printf("Wednesday"); } else if(n==4) { printf("Thursday"); } else if(n==5) { printf("Friday"); } else if(n==6) { printf("Saturday"); } else if(n==7) { printf("Sunday"); } else { printf("Invalid input"); } return 0; }Output 1:
Enter day number between 1-7: 4
Thursday
Output 2:
Thursday
Enter day number between 1-7: 8
Invalid input
In output 1 when user enters 4 the if statements will be executed likeInvalid input
if (4==1) which is false hence moves to the next condition
if (4==2) which is again false hence moves to the next condition
if (4==3) which is also false hence moves to the next condition
if (4==4) which is true hence statements of condition 4 will execute and will print Thursday
In output 2 when user enters 8 the if statements will execute like
if (8==1) which is false hence moves to the next condition
if (8==2) which is also false hence moves to the next condition
if (8==3) which is also false hence moves to the next condition
if (8==4) which is also false hence moves to the next condition
if (8==5) which is also false hence moves to the next condition
if (8==6) which is also false hence moves to the next condition
if (8==7) which is also false hence moves to the next condition
Since all if conditions are false hence the body of else will execute which will print Invalid choice.
Nested if
Nested if is the combined version of all if and is the most complex format of all if. In nested if more than one conditions are checked one after another to executed a specific statement.Syntax of nested if: if (condition1)
{
statement/s that will execute if the condition 1 is true.
if (condition2)
{
statement/s that will execute if both condition 1 and condition 2 are true.
}
else
{
statement/s that will execute when condition 1 is true but condition 2 is false.
}
}
else
{
statement/s that will execute if the condition 1 is false.
{
statement/s that will execute if condition 1 is false but condition 2 is true.
}
else
{
statement/s that will execute when both condition 1 and condition 2 are false.
}
}
Lets see a program to demonstrate nested if:
Write a program to input three numbers and find maximum between all.
#include <stdio.h> int main() { int n1, n2, n3; printf("Enter any three numbers :\n"); scanf("%d %d %d", &n1, &n2, &n3); if(n1>n2) { if(n1>n3) { printf("n1 = %d is max.", n1); } else { printf("n3 = %d is max.", n3); } } else { if(n2>n3) { printf("n2 = %d is max.", n2); } else { printf("n3 = %d is max.", n3); } } return 0; }Output 1:
Enter any three numbers :
30
12
10
n1 = 30 is max.
Output 2:
30
12
10
n1 = 30 is max.
Enter any three numbers :
10
20
9
n2 = 20 is max.
Output 3:
10
20
9
n2 = 20 is max.
Enter any three numbers :
10
20
30
n3 = 30 is max.
10
20
30
n3 = 30 is max.
-
In output 1 user enters n1=30, n2=12, n3=10. The if statements will execute like.
The first outer if condition
if ( n1>n2 ) i.e ( 30>12 ) is true. Hence the first if i.e. outer if will execute skipping the outer else part.
Then the inner condition
if ( n1>n3 ) which is also true. Hence the body of inner if will execute. And prints n1 = 30 is max.
-
In output 2 user enters n1=10, n2=20, n3=9. The if statements will execute like.
The first outer if condition
if ( n1>n2 ) i.e. ( 10>20 ) is false. Hence the first outer if will be skipped and the outer else part will execute.
Then the inner condition
if ( n2>n3 ) i.e. ( 20>9 ) which is true. Hence the inner if will execute skipping the inner else part. And prints n2 = 20 is max.
-
In output 3 user enters n1=10, n2=20, n3=30. The if statements will execute like.
The first outer if condition
if ( n1>n2 ) i.e. ( 10>20 ) is false. Hence the first outer if will be skipped and outer else part will execute.
Then the inner condition
if ( n2>n3 ) i.e. ( 20>30 ) is also false. Hence the inner if part will also be skipped executing the inner else part. And prints n3 = 30 is max.
You may also like
Problems based on if else to practice
Control statements in C
Switch case in C
List of all ASCII character codes
Problems based on if else to practice
Control statements in C
Switch case in C
List of all ASCII character codes