Introduction
Fundamentals
C Flow Control
C Functions
C Arrays
If-Else Statement in C Language
In C, the if-else statements are used to perform operations based on a specific conditions. If and only if the given condition is true, the operations specified in the if block are performed. In this tutorial, we will learn about various if, if-else and nested if-else statements with the help of various examples.
Conditional Statements in C programming are used to make decisions based on the conditions. In C Language, statements executes sequentially when there is no condition around. If we put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision making in 'C'.
1. If Statement in C:
The syntax of the if statement in C programming is:
if (condition) {
//code
}
How If statement works in C?
The if statement evaluates the test expression inside the parenthesis (). If the test expression is evaluated to false, statements inside the body of if are not executed.
Example 1: if statement
// Program to display a number if given number is less than 10
#include <stdio.h>
int main() {
int number;
printf("Enter an number: ");
scanf("%d", &number);
// true if number is less than 10
if (number < 10) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
Output :
Enter an number: 9 You entered 9. The if statement is easy.
When user enters 9, the test expression number < 10 is evaluated to true. Hence, You entered 9 is displayed on the screen.
Output :
Enter an number: 11 The if statement is easy.
When the user enters 11, the test expression number < 10 is evaluated to false and the statement inside the body of if is not executed
2. If-Else Statement in C:
The else block is optional in the if statement. The if-else statement has the following syntax:
if (Condition) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
How if-else statement works?
If the test expression is found to be true,
- Statements inside the if body are performed.
- Statements inside the body of else are not executed.
If the test expression is evaluated to false,
- statements inside the body of else are executed
- statements inside the body of if are skipped from execution.
Example 2: if-else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an number: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even number.",number);
}
else {
printf("%d is an odd number.",number);
}
return 0;
}
Output :
Enter an integer: 9 9 is an odd integer.
When a user enters 9, the test expression number%2==0 returns false. As a result, the statement within the body of else is performed.
3. If-Else-If Ladder Statement in C:
Depending on whether the test expression is true or false, the if-else statement executes two separate programm. Sometimes a decision must be made between more than two possibilities.
We can use the if-else ladder to compare multiple test expressions and execute different statements.
Syntax of if else-if Ladder
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
}
Example 3: if else-if Ladder statement
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%i", &age);
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
} else if (age < 21) {
printf("You need to be over 21\n");
} else {
printf("You are over 18 and older than 21 so you can continue \n");
}
}
Output :
Please enter your age: 11 You need to be over 18 years old to continue
Output :
Please enter your age: 22 You need to be over 21
Output :
Please enter your age: 24 You are over 18 and older than 21 so you can continue
4. Nested if-else in C Language:
An if-else statement can be included inside the body of another if-else statement.
Example 4: Nested if-else
#include<stdio.h>
int main() {
#include<stdio.h>
int main() {
int num;
printf("Enter an number: ");
scanf("%d",&num);
if (num < 10) {
if (num == 1) {
printf("The value is: %d\n", num);
} else {
printf("The value is greater than 1");
}
} else {
printf("The value is greater than 10");
}
return 0;
}
Output :
Enter an number: 14 The value is greater than 10
Output :
Enter an number: 1 The value is: 1
Output :
Enter an number: 5 The value is greater than 1