Conditionals are fundamental control structures in programming that allow you to execute certain pieces of code based on whether a condition is true or false. This module will cover the basics of conditionals, including if statements, else statements, and else-if (elif) statements.
Key Concepts
- If Statements: Execute a block of code if a specified condition is true.
- Else Statements: Execute a block of code if the same condition is false.
- Else-If (Elif) Statements: Check multiple conditions in sequence.
If Statements
An if statement evaluates a condition and executes a block of code if the condition is true.
Syntax
Example
Explanation
- The condition
age >= 18is evaluated. - If the condition is true, the code block inside the
ifstatement is executed, printing "You are an adult."
Else Statements
An else statement can be used to execute a block of code if the condition in the if statement is false.
Syntax
if condition:
# Block of code to execute if condition is true
else:
# Block of code to execute if condition is falseExample
Explanation
- The condition
age >= 18is evaluated. - If the condition is true, the first block of code is executed.
- If the condition is false, the code block inside the
elsestatement is executed, printing "You are a minor."
Else-If (Elif) Statements
An elif statement allows you to check multiple conditions in sequence.
Syntax
if condition1:
# Block of code to execute if condition1 is true
elif condition2:
# Block of code to execute if condition2 is true
else:
# Block of code to execute if none of the above conditions are trueExample
age = 20
if age < 13:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
else:
print("You are an adult.")Explanation
- The condition
age < 13is evaluated first. - If the first condition is false, the next condition
age < 18is evaluated. - If both conditions are false, the code block inside the
elsestatement is executed, printing "You are an adult."
Practical Exercises
Exercise 1: Basic If Statement
Task: Write a program that checks if a number is positive.
Solution:
Exercise 2: If-Else Statement
Task: Write a program that checks if a number is even or odd.
Solution:
Exercise 3: Elif Statement
Task: Write a program that categorizes a person's age group.
age = 25
if age < 13:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
elif age < 65:
print("You are an adult.")
else:
print("You are a senior.")Solution:
age = 25
if age < 13:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
elif age < 65:
print("You are an adult.")
else:
print("You are a senior.")Common Mistakes and Tips
- Indentation Errors: Ensure that the code blocks inside
if,else, andelifstatements are properly indented. - Logical Errors: Double-check the conditions to ensure they are logically correct.
- Order of Conditions: In
elifstatements, the order of conditions matters. Make sure to place more specific conditions before more general ones.
Conclusion
In this section, you learned about the fundamental control structures known as conditionals. You now understand how to use if, else, and elif statements to control the flow of your program based on different conditions. Practice these concepts with the provided exercises to reinforce your understanding. Next, we will delve into loops, another essential control structure in programming.
