Conditional Statements (if, else if, else, switch)
3.3.1 Conditional Statements (if, else if, else, switch)
Imagine you're at a crossroads, and you need to decide which way to go. Your decision depends on a condition, like "If the sign says 'Beach', go left. Otherwise, go straight."
Conditional Statements are programming instructions that allow your program to make decisions based on whether a certain condition is true or false. They let your code choose different paths of execution.
Here are the main types of conditional statements:
- if statement:
- This is the simplest decision-maker. It says: "IF a condition is true, THEN do this specific action."
- If the condition is false, the action is skipped.
Example:
IF (it is raining) THEN
Take an umbrella.
In code:
weather = "raining"
if weather == "raining":
print("Take an umbrella.")
- if-else statement:
- This gives your program two options. It says: "IF a condition is true, THEN do this action. ELSE (otherwise), do that different action."
- Only one of the two actions will ever happen.
Example:
IF (you are hungry) THEN
Eat a snack.
ELSE
Go play outside.
In code:
is_hungry = True
if is_hungry:
print("Eat a snack.")
else:
print("Go play outside.")
- if-else if-else (or elif in Python) statement:
- This allows your program to check multiple conditions in order. It says: "IF condition 1 is true, do this. ELSE IF condition 2 is true, do that. ELSE (if none of the above are true), do something else."
- The computer checks each condition one by one. As soon as it finds a true condition, it performs that action and then skips all the other else if and else parts.
Example:
IF (grade is A) THEN
You get a gold star!
ELSE IF (grade is B) THEN
You get a silver star!
ELSE
Keep trying!
In code (Python example):
grade = "B"
if grade == "A":
print("You get a gold star!")
elif grade == "B": # Only checked if grade is NOT "A"
print("You get a silver star!")
else: # Only runs if grade is NOT "A" and NOT "B"
print("Keep trying!")
- switch statement (in languages like Java, C++, C#):
- This is a special type of conditional statement used when you have many possible exact values for a single variable, and you want to do different things for each value.
- It's like having a menu: "Choose option 1 for pizza, option 2 for pasta, option 3 for salad."
- It's often cleaner than a long chain of if-else if statements when checking for exact matches.
Example (Java concept):
// Imagine 'dayOfWeek' is a number from 1 (Monday) to 7 (Sunday)
switch (dayOfWeek) {
case 1:
System.out.println("It's Monday!");
break; // Stop here
case 5:
System.out.println("It's Friday!");
break;
default: // If dayOfWeek is none of the above
System.out.println("It's a weekday or weekend.");
}
- (Note: Python does not have a direct switch statement, but uses if-elif-else or dictionaries for similar logic.)
Conditional statements are essential for making programs smart and able to respond differently to different situations.
Bibliography:
- W3Schools - Python If...Else: https://www.w3schools.com/python/python_conditions.asp
- GeeksforGeeks - Conditional Statements in C/C++: https://www.geeksforgeeks.org/cpp/decision-making-c-cpp/
- Wikipedia - Conditional (computer programming):
https://en.wikipedia.org/wiki/Conditional_(computer_programming))