In C language, the if
, else if
, and else
statements are used for decision-making. They allow the program to execute specific blocks of code based on certain conditions, making the program more dynamic and capable of handling various scenarios.
if
Statement
The if
statement is used to test a condition. If the condition is true
(non-zero), the block of code inside the if
statement is executed. If the condition is false
(zero), the program skips the if
block.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int a = 10;
if (a > 5) {
printf("a is greater than 5\n");
}
In this example, since a
is 10
(which is greater than 5
), the message "a is greater than 5"
will be printed.
else if
Statement
The else if
statement allows you to test multiple conditions after the initial if
. It is used when you have several conditions to check.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
}
// Additional else if statements can be added as needed
Example:
int a = 10;
if (a > 15) {
printf("a is greater than 15\n");
} else if (a > 10) {
printf("a is greater than 10 but less than or equal to 15\n");
} else if (a == 10) {
printf("a is equal to 10\n");
}
In this example, the condition a == 10
is true
, so the output will be "a is equal to 10"
.
else
Statement
The else
statement provides a block of code to execute if none of the previous conditions (if
or else if
) are true
. It acts as a "catch-all" for scenarios where all other conditions fail.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the above conditions are true
}
Example:
int a = 5;
if (a > 10) {
printf("a is greater than 10\n");
} else if (a > 5) {
printf("a is greater than 5 but less than or equal to 10\n");
} else {
printf("a is less than or equal to 5\n");
}
Since a
is 5
and does not meet the conditions in the if
or else if
statements, the output will be "a is less than or equal to 5"
.
Complete Example
Here's a complete example that uses if
, else if
, and else
:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
} else if (num < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;
}
In this example:
- If the user enters a positive number, the output will be
"The number is positive."
. - If the user enters a negative number, the output will be
"The number is negative."
. - If the user enters
0
, the output will be"The number is zero."
.
Summary
if
statement: Executes a block of code if the given condition istrue
.else if
statement: Tests additional conditions if the previousif
orelse if
wasfalse
.else
statement: Executes a block of code if none of theif
orelse if
conditions aretrue
.
The if-else if-else
structure provides flexibility in decision-making, allowing you to handle multiple conditions efficiently in your C programs.