Increment and decrement in Go - Time & Space Complexity
Increment and decrement operations change a value by one step. We want to see how these operations affect the time it takes to run a program.
How does the number of increments or decrements grow when we repeat them many times?
Analyze the time complexity of the following code snippet.
package main
func countUp(n int) int {
total := 0
for i := 0; i < n; i++ {
total++
}
return total
}
This code counts up from zero to n by adding one each time in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Incrementing the variable
totalinside the loop. - How many times: Exactly
ntimes, once per loop cycle.
Each time we increase n, the loop runs that many times, doing one increment each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The number of increments grows directly with n. Double n, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the size of n.
[X] Wrong: "Incrementing inside a loop is always very fast, so it doesn't affect time much."
[OK] Correct: Even simple increments add up when repeated many times, so the total time depends on how many increments happen.
Understanding how simple steps like increments add up helps you explain how loops affect program speed. This skill shows you can think about how code grows with input size.
"What if we replaced the increment total++ with a function call inside the loop? How would the time complexity change?"
Practice
i++ do in Go?Solution
Step 1: Understand the operator meaning
The++operator in Go adds 1 to the variable it is used on.Step 2: Apply to variable i
Usingi++increases the value of i by exactly 1.Final Answer:
Increases the value of i by 1 -> Option BQuick Check:
i++means add 1 [OK]
- Confusing ++ with --
- Thinking ++ multiplies or divides
- Using ++ as a function
count by 1 in Go?Solution
Step 1: Identify decrement syntax in Go
In Go,variable--is the correct way to subtract 1 from a variable.Step 2: Check each option
count-- usescount--, which is correct. --count is invalid prefix decrement in Go. count++ increases count. count =- 1 is invalid syntax.Final Answer:
count-- -> Option AQuick Check:
Use--to subtract one [OK]
- Using --count instead of count--
- Confusing ++ and --
- Using count += 1 to decrement
package main
import "fmt"
func main() {
x := 5
x++
x--
x++
fmt.Println(x)
}Solution
Step 1: Trace the increments and decrements
Start with x = 5. Then x++ makes x = 6. Then x-- makes x = 5 again. Then x++ makes x = 6.Step 2: Print the final value
The final value of x is 6, sofmt.Println(x)prints 6.Final Answer:
6 -> Option DQuick Check:
5 +1 -1 +1 = 6 [OK]
- Ignoring one increment or decrement
- Thinking increments happen before assignment
- Confusing output with syntax error
package main
func main() {
var n int = 10
n++
n--
n = n++
}Solution
Step 1: Understand usage of n++ in Go
In Go,n++andn--are statements that increment or decrement n by 1. They cannot be used as expressions.Step 2: Analyze the assignment n = n++
Assigningn = n++tries to usen++as a value, which is invalid in Go and causes a compile error.Final Answer:
Assigning n = n++ causes a compile error -> Option AQuick Check:
n++ is a statement, not an expression [OK]
- Trying to assign n = n++
- Thinking n-- is invalid on int
- Ignoring Go's statement-only rule for ++/--
for i := 3; i > 0; ??? {
fmt.Println(i)
}Solution
Step 1: Understand the loop goal
The loop should count down from 3 to 1, so i must decrease each time.Step 2: Choose the correct decrement expression
Usingi--decreases i by 1 each loop, matching the goal. Options A, B, and C are wrong: A and B increase i, C is invalid prefix--iin Go.Final Answer:
i-- -> Option CQuick Check:
Use i-- to count down in loops [OK]
- Using i++ when counting down
- Using i += 1 instead of i--
- Confusing decrement syntax in for loops
Ready to prove it?
Pass the test and this pattern moves unknown
