If statement in Java - Time & Space Complexity
We want to see how the time it takes to run an if statement changes as the input changes.
Does the if statement make the program slower when the input grows?
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 condition check.
- How many times: Exactly once per function call.
Whether the input is small or large, the if statement runs just once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter how big the input is.
Time Complexity: O(1)
This means the if statement takes the same amount of time no matter the input size.
[X] Wrong: "If statements take longer when the input number is bigger."
[OK] Correct: The if condition is checked only once, so input size does not affect its speed.
Understanding that simple decisions like if statements run in constant time helps you explain how your code behaves as inputs grow.
"What if we added a loop inside the if statement that runs n times? How would the time complexity change?"
Practice
if statement do in Java?Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and decides whether to run some code based on that condition.Step 2: Match the description to the options
It runs code only if a condition is true. correctly says it runs code only if the condition is true, which matches the if statement's behavior.Final Answer:
It runs code only if a condition is true. -> Option DQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if stores data
- Mixing if with class creation
Solution
Step 1: Recall Java if statement syntax
Java requires parentheses around the condition and braces are optional if only one statement follows.Step 2: Check each option
if (condition) System.out.println("Yes"); uses parentheses and a single statement without braces, which is valid syntax.Final Answer:
if (condition) System.out.println("Yes"); -> Option AQuick Check:
Parentheses needed around condition [OK]
- Omitting parentheses around condition
- Using colons instead of braces
- Adding 'then' keyword
int x = 10;
if (x > 5) {
System.out.println("Big number");
} else {
System.out.println("Small number");
}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 "Big number" because the condition is true.Final Answer:
Big number -> Option BQuick Check:
10 > 5 is true, prints "Big number" [OK]
- Choosing else output when condition is true
- Thinking no output if condition true
- Confusing syntax with output
int num = 7;
if num > 5 {
System.out.println("Greater");
}Solution
Step 1: Check if statement syntax
Java requires parentheses around the condition in an if statement.Step 2: Identify the missing parentheses
The code has if num > 5 without parentheses, which causes a syntax error.Final Answer:
Missing parentheses around condition -> Option CQuick Check:
If condition must be in parentheses [OK]
- Forgetting parentheses around condition
- Adding semicolon after if line
- Misnaming 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"); } correctly uses n % 2 == 0 to check evenness and prints "Even" or "Odd" accordingly.Final Answer:
if (n % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } -> Option AQuick Check:
Use modulus == 0 for even check [OK]
- Using assignment = instead of comparison ==
- Checking division instead of remainder
- Reversing even and odd conditions
Ready to prove it?
Pass the test and this pattern moves unknown
