1 Answers
🧠 Quick Study Guide: If/Else Statements
- 🎯 Purpose: If/Else statements allow programs to make decisions and execute different blocks of code based on whether a specified condition is true or false.
- ✍️ Basic Structure: An
ifblock executes if its condition is true. An optionalelseblock executes if theifcondition is false. - ➕ Else If (
elif/else if): Used to check multiple conditions sequentially. If the firstifis false, it checks theelse if, and so on. Only the first true condition's block is executed. - 🧩 Nested If/Else: Placing an if/else statement inside another if/else statement. Useful for complex, multi-layered decision-making.
- ⚖️ Conditions: Conditions are expressions that evaluate to a boolean (true or false). They often use comparison operators (
==,!=,>,<,>=,<=) and logical operators (AND/&&,OR/||,NOT/!). - 📏 Indentation: In many languages (like Python), indentation defines the code blocks within if/else statements. In others (like C++, Java, JavaScript), curly braces
{}are used.
📝 Practice Quiz
1. What is the primary purpose of an 'if' statement in programming?
- A) To define a loop that repeats a block of code.
- B) To declare a new variable.
- C) To execute a block of code only if a specified condition is true.
- D) To print output to the console.
2. Which keyword is used to provide an alternative block of code to execute when the 'if' condition is false?
- A)
while - B)
for - C)
else - D)
then
3. Consider the following Python code snippet:
x = 10
if x > 15:
print("A")
elif x > 5:
print("B")
else:
print("C")
What will be printed to the console?
- A) A
- B) B
- C) C
- D) Nothing
4. In a language like Java or C++, what punctuation typically encloses the code block associated with an 'if' or 'else' statement?
- A) Parentheses
() - B) Square brackets
[] - C) Curly braces
{} - D) Angle brackets
<>
5. Which of the following is an example of a valid condition for an 'if' statement?
- A)
x is equal to 5 - B)
x == 5 - C)
x = 5 - D)
if x then
6. What is the term for placing an 'if/else' statement inside another 'if' or 'else' statement?
- A) Looping
- B) Iteration
- C) Recursion
- D) Nesting
7. If you have multiple conditions to check sequentially, and you want to execute code for the first condition that is true, which construct would you typically use after an initial 'if' statement?
- A) An additional
ifstatement (withoutelse) - B) A
switchstatement (if available) orelif/else if - C) A
forloop - D) A
try-catchblock
Click to see Answers
1. C - If statements control conditional execution.
2. C - The 'else' keyword provides an alternative path.
3. B - x > 15 is false (10 > 15). x > 5 is true (10 > 5), so 'B' is printed.
4. C - Curly braces {} define code blocks in C-like languages.
5. B - == is the equality comparison operator. = is assignment.
6. D - This is known as nesting.
7. B - elif (Python) or else if (C++, Java, JavaScript) is designed for checking multiple sequential conditions.
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀