If statement in Go - Time & Space Complexity
We want to see how the time to run an if statement changes as the input changes.
Does the if statement take more time when inputs grow?
Analyze the time complexity of the following code snippet.
func checkNumber(n int) string {
if n > 0 {
return "Positive"
} else if n == 0 {
return "Zero"
} else {
return "Negative"
}
}
This code checks if a number is positive, zero, or negative and returns a string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single if-else check.
- How many times: Exactly once per function call.
The if statement runs once no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the input number changes.
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 number is bigger."
[OK] Correct: The if statement only checks conditions once, so size or value does not affect time.
Understanding that simple decisions like if statements run in constant time helps you explain code efficiency clearly and confidently.
"What if we added a loop around the if statement? How would the time complexity change?"
Practice
if statement do in Go?Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and runs code only when that condition is true.Step 2: Compare options with the if statement behavior
Only "It runs code only if a condition is true." describes running code based on a condition being true.Final Answer:
It runs code only if a condition is true. -> Option AQuick Check:
If statement = runs code if true [OK]
- Confusing if with loops
- Thinking if stores data
- Mixing if with function definitions
Solution
Step 1: Recall Go if statement syntax
Go usesif condition { code }without parentheses or 'then'.Step 2: Check each option
if x > 10 { fmt.Println("Big") } matches Go syntax exactly. Options A, C, and D have syntax errors.Final Answer:
if x > 10 { fmt.Println("Big") } -> Option BQuick Check:
Go if syntax = if condition { } [OK]
- Adding 'then' keyword
- Using parentheses around condition
- Using colon instead of braces
package main
import "fmt"
func main() {
x := 5
if x > 3 {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
}Solution
Step 1: Check the condition in the if statement
The variable x is 5, and the condition is x > 3, which is true.Step 2: Determine which block runs
Since the condition is true, the code inside the if block runs, printing "Yes".Final Answer:
Yes -> Option DQuick Check:
x=5 > 3 is true, prints "Yes" [OK]
- Confusing which block runs
- Thinking it prints the variable
- Assuming syntax error due to else
package main
import "fmt"
func main() {
x := 10
if x > 5
fmt.Println("Big")
}Solution
Step 1: Check if statement syntax
In Go, the if condition must be followed by braces { } to group the code.Step 2: Identify the missing part
The code after if lacks braces, causing a syntax error.Final Answer:
Missing braces { } after if condition -> Option CQuick Check:
If needs braces { } after condition [OK]
- Omitting braces after if
- Adding unnecessary semicolons
- Misunderstanding import errors
n. Which code correctly uses if statements to do this?Solution
Step 1: Understand the conditions for each case
Positive means n > 0, zero means n == 0, negative means n < 0.Step 2: Check each option's logic
if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } correctly checks n > 0, then n == 0, else negative. if n > 0 { fmt.Println("Positive") } if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } has two separate ifs causing multiple prints. if n > 0 { fmt.Println("Positive") } else if n <= 0 { fmt.Println("Negative") } else { fmt.Println("Zero") } swaps zero and negative logic. if n >= 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } wrongly treats n >= 0 as positive.Final Answer:
if n > 0 { fmt.Println("Positive") } else if n == 0 { fmt.Println("Zero") } else { fmt.Println("Negative") } -> Option AQuick Check:
Correct if-else chain matches conditions [OK]
- Using multiple ifs causing multiple outputs
- Mixing condition order
- Wrong comparison operators
Ready to prove it?
Pass the test and this pattern moves unknown
