In C programming, the if-else statement is one of the most fundamental control structures that allows you to execute specific blocks of code based on the evaluation of conditions. These conditions are typically expressions that evaluate to either true (non-zero) or false (zero). By using if-else statements, you can direct the flow of your program and make decisions at runtime.
The if-else statement is essential for implementing logic that requires different actions based on varying conditions. It helps in:
1. Decision Making: Allows the program to choose different paths of execution based on conditions.
2. Control Flow: Manages the flow of the program by executing certain blocks of code while skipping others.
3. Error Handling: Helps in handling errors and exceptional cases by checking conditions and taking appropriate actions.
4. User Interaction: Facilitates interactive programs by responding differently to various user inputs.
• If statement
• If-else statement
• If else-if ladder
• Nested if
The if statement executes a block of code if the specified condition is true. If the condition is false, the code block is skipped.
if (condition) {
// code to be executed if condition is true
}
#include <stdio.h>
int main() {
int num = 10;
if (num > 5) {
printf("Number is greater than 5\n");
}
return 0;
}
Number is greater than 5
The if-else statement allows you to execute one block of code if the condition is true, and another block of code if the condition is false.
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
#include <stdio.h>
int main() {
int num = 4;
if (num > 5) {
printf("Number is greater than 5\n");
} else {
printf("Number is less than or equal to 5\n");
}
return 0;
}
Number is less than or equal to 5
The if-else-if ladder allows you to test multiple conditions sequentially. The first true condition's block is executed, and the rest are skipped.
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 conditions are false
}
#include <stdio.h>
int main() {
int num = 10;
if (num == 0) {
printf("Number is zero\n");
} else if (num > 0 && num <= 5) {
printf("Number is between 1 and 5\n");
} else if (num > 5 && num <= 10) {
printf("Number is between 6 and 10\n");
} else {
printf("Number is greater than 10\n");
}
return 0;
}
Number is between 6 and 10
A nested if statement is an if or else statement inside another if or else statement. This allows for more complex decision-making processes.
if (condition1) {
// code to be executed if condition1 is true
if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if condition2 is false
}
} else {
// code to be executed if condition1 is false
}
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive\n");
if (num % 2 == 0) {
printf("Number is
even\n");
} else {
printf("Number is
odd\n");
}
} else {
printf("Number is non-positive\n");
}
return 0;
}
Number is positive
Number is even