Control Flow: Conditional Statements (if, else, switch)
Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.
Control Flow: Conditional Statements (if-else if-else) in C
Conditional statements are fundamental building blocks in programming that allow your code to execute different blocks of code based on whether a certain condition is true or false. The if-else if-else
construct is used when you need to check multiple conditions sequentially.
Understanding the if-else if-else
Construct
The if-else if-else
statement allows you to chain together multiple conditions. The program evaluates each condition in order, and executes the block of code associated with the first condition that evaluates to true. If none of the conditions are true, the code within the final else
block is executed (if it's present).
Syntax
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition1 is false AND condition2 is true
} else if (condition3) {
// Code to execute if condition1 and condition2 are false AND condition3 is true
} else {
// Code to execute if none of the above conditions are true
}
Explanation
if (condition1)
: The first condition to be evaluated. Ifcondition1
evaluates to true (non-zero), the code block immediately following theif
statement is executed.else if (condition2)
: Ifcondition1
is false (zero), the program checkscondition2
. Ifcondition2
is true, its corresponding code block is executed. You can have multipleelse if
blocks to check multiple conditions.else
: This is the optional "catch-all" block. If none of the precedingif
orelse if
conditions are true, the code within theelse
block is executed. If you omit theelse
block and none of the conditions are true, the program simply proceeds to the next statement after theif-else if
chain.
Example in C
#include <stdio.h>
int main() {
int age = 25;
if (age < 13) {
printf("You are a child.\n");
} else if (age < 20) {
printf("You are a teenager.\n");
} else if (age < 65) {
printf("You are an adult.\n");
} else {
printf("You are a senior citizen.\n");
}
return 0;
}
Explanation of the Example
In this example, the program checks the value of the age
variable against several ranges:
- If
age
is less than 13, it prints "You are a child." - If
age
is NOT less than 13, it checks if it's less than 20. If so, it prints "You are a teenager." - If
age
is NOT less than 13 and NOT less than 20, it checks if it's less than 65. If so, it prints "You are an adult." - If
age
is NOT less than 13, NOT less than 20, and NOT less than 65, it prints "You are a senior citizen."
Since age
is 25, the condition age < 65
is true, so the output will be "You are an adult."
Key Considerations
- Order Matters: The order of your conditions is crucial. The first condition that evaluates to true will have its code block executed, and the rest will be skipped. For example, if the first condition was
age < 65
, then the code block for adults would always be executed for valid ages, and the other conditions would never be checked. - Efficiency: While
if-else if-else
is powerful, using too manyelse if
blocks can sometimes make your code less readable and potentially less efficient. Consider alternative control flow structures likeswitch
statements when dealing with a large number of discrete, mutually exclusive conditions (especially when comparing a single variable against many possible values). - Boolean Expressions: The conditions within the
if
andelse if
statements must be boolean expressions (expressions that evaluate to either true or false). In C, 0 represents false, and any non-zero value represents true.