Conditional Statements- Post Test Review with Answer Key
What Are Conditional Statements?
Conditional statements are the backbone of decision-making in programming. They let your code choose different paths based on whether something is true or false. Without them, software would be completely rigid.
Most programming exams throw several questions at you on this topic. Here's everything you need to know before you walk into that test room.
The Three Core Types You Must Know
1. Simple If Statements
This is the most basic form. The code inside only runs if the condition evaluates to true.
Syntax pattern:
if (condition) {
// code runs here
}
The condition must be a boolean expression. Anything that returns true or false.
2. If-Else Statements
When the condition is false, you can specify an alternative block to execute instead.
Syntax pattern:
if (condition) {
// runs if true
} else {
// runs if false
}
This covers all binary outcomes. Either A happens, or B happens.
3. Else-If Chains
When you have multiple conditions to check in sequence, use else-if.
Syntax pattern:
if (condition1) {
// runs if condition1 is true
} else if (condition2) {
// runs if condition1 is false, condition2 is true
} else {
// runs if all conditions are false
}
You can chain as many else-if blocks as you need. The first true condition wins.
Comparison Table: If vs Else-If vs Switch
| Feature | If-Else Chain | Switch Statement |
|---|---|---|
| Condition type | Any boolean expression | Single variable, specific values |
| Flexibility | High (ranges, comparisons) | Limited (exact matches only) |
| Fall-through behavior | No (one block executes) | Yes (without break statements) |
| Best used for | Complex logic, ranges | Menu selections, discrete values |
Most exam questions will ask you to convert between if-else and switch. Know both directions.
Common Mistakes Students Make
- Forgetting curly braces when only one statement follows the condition
- Using
=instead of==for comparisons - Putting a semicolon after the condition — this makes the if statement do nothing
- Checking the wrong variable or using incorrect operators
That semicolon mistake is especially nasty. The code looks correct but silently fails.
Getting Started: How to Approach Conditional Problems
Follow this step-by-step method when you see a conditional question:
- Read the problem twice. Identify what inputs are given and what output is expected.
- Determine the number of branches. How many possible outcomes exist?
- Write the conditions in plain English first. "If grade is greater than or equal to 60, pass."
- Translate to code. Use the appropriate if-else structure.
- Test with edge cases. What happens at the boundary values?
This approach works for any programming language. The logic is identical whether you're writing Java, Python, C++, or JavaScript.
Practice Questions with Answer Key
Question 1
What will this code output?
int x = 10;
if (x > 5) {
System.out.println("A");
} else {
System.out.println("B");
}
Answer: A
Explanation: x equals 10, which is greater than 5. The condition is true, so "A" prints.
Question 2
Find the error:
int score = 85;
if (score = 100);
{
System.out.println("Perfect!");
}
Answer: Two errors. First, = should be ==. Second, remove the semicolon after the condition. The corrected code:
int score = 85;
if (score == 100) {
System.out.println("Perfect!");
}
Question 3
Write code that prints "Pass" if grade is 60 or above, "Fail" otherwise.
Answer:
if (grade >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Question 4
Convert this switch to an if-else chain:
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other"); break;
}
Answer:
if (day == 1) {
System.out.println("Monday");
} else if (day == 2) {
System.out.println("Tuesday");
} else {
System.out.println("Other");
}
Question 5
What does this code output?
int a = 5, b = 10;
if (a > b) {
System.out.println("A");
} else if (a == b) {
System.out.println("C");
} else {
System.out.println("D");
}
Answer: D
Explanation: 5 is not greater than 10, and 5 does not equal 10. Only the else block executes.
Key Takeaways for Your Test
- Conditional statements direct program flow based on boolean conditions
- Always use
==for comparison,=for assignment - Never put a semicolon directly after your condition in an if statement
- Switch statements work only with discrete values, not ranges
- Read questions carefully — trace through each branch manually
Go through the practice questions again without looking at the answers. If you can get them right, you're ready. If not, focus on the sections where you struggled.