If–else statement in Go - Time & Space Complexity
We want to see how the time taken by an if-else statement changes as the input changes.
Does the program take longer if the input is bigger?
Analyze the time complexity of the following code snippet.
func checkNumber(n int) string {
if n > 0 {
return "Positive"
} else {
return "Non-positive"
}
}
This code checks if a number is positive or not and returns a message.
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-else statement runs once no matter what the input number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The time stays the same no matter how big the input is.
Time Complexity: O(1)
This means the time to run the if-else does not grow with input size; it stays constant.
[X] Wrong: "If-else takes longer when the number is bigger because it has to check more."
[OK] Correct: The if-else only checks once and does not loop or repeat, so input size does not affect it.
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-else that runs n times? How would the time complexity change?"
Practice
if-else statement do in Go?Solution
Step 1: Understand the purpose of if-else
An if-else statement checks a condition and runs code based on whether it is true or false.Step 2: Compare with other options
Repeating code is done by loops, functions define reusable code, and variables store data, so these are not correct.Final Answer:
It chooses between two paths based on a condition. -> Option DQuick Check:
If-else = choose path [OK]
- Confusing if-else with loops
- Thinking if-else defines functions
- Mixing if-else with variable declaration
Solution
Step 1: Check Go if-else syntax
Go uses braces {} for blocks and no parentheses or colons around conditions.Step 2: Identify correct syntax
if x > 10 { fmt.Println("Big") } else { fmt.Println("Small") } matches Go syntax exactly. Options A, B, and D use wrong keywords or punctuation.Final Answer:
if x > 10 { fmt.Println("Big") } else { fmt.Println("Small") } -> Option AQuick Check:
Go if-else uses braces {} [OK]
- Using 'then' keyword
- Adding colons after conditions
- Writing 'elseif' instead of 'else if'
package main
import "fmt"
func main() {
x := 5
if x > 10 {
fmt.Println("Greater")
} else {
fmt.Println("Smaller or equal")
}
}Solution
Step 1: Check the value of x
x is assigned 5, which is not greater than 10.Step 2: Follow the if-else logic
Since x > 10 is false, the else block runs, printing "Smaller or equal".Final Answer:
Smaller or equal -> Option BQuick Check:
5 > 10? No, print else [OK]
- Assuming 5 is greater than 10
- Confusing if and else blocks
- Expecting variable value as output
package main
import "fmt"
func main() {
x := 7
if x > 5
fmt.Println("Big")
else
fmt.Println("Small")
}Solution
Step 1: Check if-else block syntax
In Go, if and else blocks must use braces {} to group statements.Step 2: Identify missing braces
The code lacks braces around the print statements, causing a syntax error.Final Answer:
Missing braces {} around if and else blocks -> Option AQuick Check:
Go requires braces for blocks [OK]
- Omitting braces in if-else
- Thinking semicolons are needed after conditions
- Confusing import errors with syntax errors
Solution
Step 1: Understand how to check even numbers
A number is even if remainder when divided by 2 is zero (num % 2 == 0).Step 2: Check each option's condition
if num % 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } uses correct equality check. if num / 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } divides instead of modulo. if num % 2 = 0 { fmt.Println("Even") } else { fmt.Println("Odd") } uses assignment (=) instead of comparison (==). if num % 2 != 0 { fmt.Println("Even") } else { fmt.Println("Odd") } reverses logic.Final Answer:
if num % 2 == 0 { fmt.Println("Even") } else { fmt.Println("Odd") } -> Option CQuick Check:
Modulo equals zero means even [OK]
- Using / instead of %
- Using = instead of ==
- Reversing condition logic
Ready to prove it?
Pass the test and this pattern moves unknown
