If–else statement in Java - Time & Space Complexity
We want to see how the time to run an if-else statement changes as the input changes.
Does the choice inside the if-else affect how long the program takes?
Analyze the time complexity of the following code snippet.
int checkNumber(int n) {
if (n > 0) {
return 1;
} else {
return -1;
}
}
This code checks if a number is positive or not and returns a value accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per call, no loops or repeated steps.
Whether the input is small or large, the program only does one check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The number of operations stays the same no matter the input size.
Time Complexity: O(1)
This means the program takes the same amount of time no matter what number you give it.
[X] Wrong: "If-else statements take longer when the number is bigger."
[OK] Correct: The if-else only checks once, so the size of the number does not change how long it takes.
Understanding that simple decisions like if-else run in constant time helps you explain how your code behaves clearly and confidently.
"What if we added a loop inside the if or else block? How would the time complexity change?"
Practice
if-else statement do in Java?Solution
Step 1: Understand the purpose of if-else
An if-else statement lets the program decide between two paths depending on a condition.Step 2: Compare with other options
Repeating code is done by loops, storing values is done by collections, and defining classes is unrelated.Final Answer:
It chooses between two actions based on a condition. -> Option DQuick Check:
If-else = choose between two paths [OK]
- Confusing if-else with loops
- Thinking if-else stores data
- Mixing if-else with class definitions
Solution
Step 1: Recall Java syntax rules
Java requires parentheses around the condition and curly braces for code blocks.Step 2: Check each option
Only if (condition) { } else { } uses parentheses and curly braces correctly without extra keywords like 'then'.Final Answer:
if (condition) { } else { } -> Option BQuick Check:
Parentheses + braces = correct syntax [OK]
- Omitting parentheses around condition
- Using 'then' keyword incorrectly
- Missing curly braces for blocks
int x = 10;
if (x > 5) {
System.out.println("Greater");
} else {
System.out.println("Smaller or equal");
}Solution
Step 1: Evaluate the condition x > 5
Since x is 10, the condition 10 > 5 is true.Step 2: Determine which block runs
The if block runs and prints "Greater"; the else block is skipped.Final Answer:
Greater -> Option AQuick Check:
10 > 5 is true, so print "Greater" [OK]
- Confusing > with <
- Thinking else block runs when if is true
- Assuming no output if condition true
int num = 7;
if num > 5 {
System.out.println("Yes");
} else {
System.out.println("No");
}Solution
Step 1: Check if statement syntax
Java requires parentheses around the condition in an if statement.Step 2: Verify other parts
Semicolon after if is not needed; curly braces are present; num is declared.Final Answer:
Missing parentheses around the condition in if statement. -> Option CQuick Check:
If condition needs () [OK]
- Omitting parentheses in if condition
- Adding unnecessary semicolon after if
- Forgetting to declare variables
int n = 4; // Your code here
Solution
Step 1: Understand how to check even numbers
A number is even if dividing by 2 leaves remainder 0, so use n % 2 == 0.Step 2: Check each option's condition
if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses correct equality check with == and modulo operator. if (n / 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses division instead of modulo. if (n % 2 = 0) { System.out.println("Even"); } else { System.out.println("Odd"); } uses assignment = instead of comparison ==. if (n % 2 != 0) { System.out.println("Even"); } else { System.out.println("Odd"); } reverses logic.Final Answer:
if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } -> Option AQuick Check:
Use modulo == 0 to check even [OK]
- Using = instead of == for comparison
- Using division instead of modulo
- Reversing even and odd logic
Ready to prove it?
Pass the test and this pattern moves unknown
