If Else in Multple Languages
If Else in Multple Languages
Basic Syntax
c
CopyEdit
if (condition) {
// Statements to execute if condition is true
} else if (other_condition) {
// Statements to execute if other_condition is true
} else {
// Statements to execute if all above conditions are false
}
Key Points
1. Condition: Enclosed in parentheses (). It usually evaluates to:
• Non-zero (true)
• Zero (false)
2. Braces { }: Required to group statements when using if-else blocks, unless the block has a
single statement (though using braces consistently is good practice).
3. Chaining: You can use multiple else if statements to check multiple conditions in sequence.
4. Example:
c
CopyEdit
#include <stdio.h>
int main() {
int x = 10;
if (x > 0) {
printf("x is positive\n");
} else if (x == 0) {
printf("x is zero\n");
} else {
printf("x is negative\n");
}
return 0;
}
Key Points
1. Indentation: Python uses indentation (spaces or tabs) to define code blocks, instead of braces.
2. Condition: In Python, expressions like x > 0 or function returns (e.g., my_func()) evaluate
to boolean values True or False.
3. Chaining: You can have as many elif statements as needed to handle multiple conditions.
4. Example:
python
CopyEdit
x = 10
if x > 0:
print("x is positive")
elif x == 0:
print("x is zero")
else:
print("x is negative")
Summary
• C uses parentheses around conditions and braces {} to enclose the code block. A condition is
true if it’s non-zero.
• Python uses indentation to signify code blocks and evaluates conditions to True or False.
Both languages support multiple branching (via else if in C or elif in Python) for checking
additional conditions.