0% found this document useful (0 votes)
12 views2 pages

If Else in Multple Languages

The document explains the syntax and key points of if-else statements in C and Python. C requires parentheses for conditions and braces for code blocks, while Python uses indentation to define blocks. Both languages allow for multiple conditions through chaining with else if or elif statements.

Uploaded by

sdas.cse21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

If Else in Multple Languages

The document explains the syntax and key points of if-else statements in C and Python. C requires parentheses for conditions and braces for code blocks, while Python uses indentation to define blocks. Both languages allow for multiple conditions through chaining with else if or elif statements.

Uploaded by

sdas.cse21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

C: If-Else Statement

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;
}

Python: If-Elif-Else Statement


Basic Syntax
python
CopyEdit
if condition:
# Statements to execute if condition is True
elif other_condition:
# Statements to execute if other_condition is True
else:
# Statements to execute if all above conditions are False

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.

You might also like