π Quick Study Guide: Conditional Statement Debugging
- π‘ Understanding Conditional Logic: Conditional statements (
if, else if/elif, else) control program flow based on whether a condition is true or false.
- π¨ Common Syntax Errors:
- π Missing colons (
:) in Python.
- π Unmatched curly braces (
{}) in C++, Java, JavaScript.
- π« Missing semicolons (
;) at the end of statements in C++, Java, JavaScript.
- π§ Logical Errors:
- β Incorrect comparison operators (e.g., using
= instead of == for comparison).
- π Flawed boolean logic (e.g., using
AND where OR is needed, or vice-versa).
- π’ Incorrect order of conditions (e.g., placing a general condition before a more specific one).
- π― Off-by-one errors in range checks.
- π οΈ Debugging Strategies:
- π¨οΈ Use
print() or console.log() statements to trace variable values and execution path.
- π Step through code line-by-line using an IDE's debugger.
- π¬ Isolate the problematic code block and test with various inputs.
- π€ Carefully review the problem statement and your logic against expected behavior.
- βοΈ Truthiness & Falsiness: Be aware of how different languages evaluate non-boolean values in a conditional context (e.g.,
0, empty strings, null, undefined are often "falsy").
π― Practice Quiz: Conditional Statements Debugging
Identify the error in each code snippet or determine its output.
Question 1:
# Python
score = 85
if score > 90
print("Excellent!")
else:
print("Good effort.")
What is the primary error in this Python code?
- Missing indentation for the
print statement.
- Incorrect comparison operator.
- Missing colon after the
if condition.
- The
else statement is unnecessary.
Question 2:
// Java
int age = 17;
String status;
if (age = 18) {
status = "Adult";
} else {
status = "Minor";
}
System.out.println(status);
What will be the output of this Java code?
- "Adult"
- "Minor"
- Compilation Error
- Runtime Error
Question 3:
// JavaScript
let temp = 25;
if (temp < 0 || temp > 30) {
console.log("Extreme temperature.");
} else if (temp >= 0 && temp <= 20) {
console.log("Cool or mild.");
} else {
console.log("Warm.");
}
What will be printed to the console?
- "Extreme temperature."
- "Cool or mild."
- "Warm."
- Nothing, it's an error.
Question 4:
# Python
x = 10
if x > 5:
if x < 15:
print("X is between 5 and 15.")
else:
print("X is not greater than 5.")
What will be the output of this Python code?
- "X is between 5 and 15."
- "X is not greater than 5."
- Syntax Error
- No output.
Question 5:
// C++
int num = 0;
if (num) { // This checks if num is non-zero
std::cout << "Number is truthy." << std::endl;
} else {
std::cout << "Number is falsy." << std::endl;
}
What will be the output of this C++ code?
- "Number is truthy."
- "Number is falsy."
- Compilation Error
- Runtime Error
Question 6:
// JavaScript
let isLoggedIn = false;
let isAdmin = true;
if (isLoggedIn && isAdmin) {
console.log("Admin logged in.");
} else if (isLoggedIn || isAdmin) {
console.log("User or Admin logged in.");
} else {
console.log("Not logged in.");
}
What will be printed to the console?
- "Admin logged in."
- "User or Admin logged in."
- "Not logged in."
- Compilation Error.
Question 7:
# Python
grade = 75
if grade >= 90:
result = "A"
elif grade >= 80:
result = "B"
elif grade >= 70:
result = "C"
else:
result = "F"
print(result)
What will be the output of this Python code?
- "A"
- "B"
- "C"
- "F"
Click to see Answers
- Question 1: C (Missing colon after
if score > 90)
- Question 2: C (Compilation Error:
age = 18 is an assignment, not a comparison, which is not allowed in a Java if condition for a primitive type. It would be age == 18.)
- Question 3: C (
temp is 25. The first condition (temp < 0 || temp > 30) is false. The second condition (temp >= 0 && temp <= 20) is false. So, it falls to the else block.)
- Question 4: A (
x is 10. x > 5 is true. Then x < 15 is true. The inner print executes. The else is associated with the outer if, but the outer if condition was true, so its else is skipped.)
- Question 5: B (In C++,
0 evaluates to false in a boolean context. Any non-zero integer evaluates to true.)
- Question 6: B (
isLoggedIn is false, isAdmin is true. isLoggedIn && isAdmin is false. isLoggedIn || isAdmin (false || true) is true. So, "User or Admin logged in." is printed.)
- Question 7: C (
grade is 75. grade >= 90 is false. grade >= 80 is false. grade >= 70 (75 >= 70) is true. So, result becomes "C".)