Recall & Review
beginner
What does the increment operator
++ do in Go?It increases the value of a variable by 1. For example,
i++ adds 1 to i.Click to reveal answer
beginner
How do you decrease a variable's value by 1 in Go?
Use the decrement operator
--. For example, i-- subtracts 1 from i.Click to reveal answer
intermediate
Can you use
++ or -- as part of an expression in Go?No. In Go,
++ and -- are statements, not expressions. You cannot write j = i++ like in some other languages.Click to reveal answer
beginner
Show a simple Go code snippet that increments a variable
count by 1.<pre>package main
import "fmt"
func main() {
count := 0
count++
fmt.Println(count) // Output: 1
}</pre>Click to reveal answer
intermediate
Why can't you write
++i or --i in Go?Go does not support prefix increment or decrement operators. Only postfix
i++ and i-- are allowed.Click to reveal answer
What does
i++ do in Go?✗ Incorrect
i++ increases the value of i by 1.
Which of these is valid in Go?
✗ Incorrect
Only i++ as a statement is valid. You cannot use ++ as part of an expression.
What happens if you write
i-- in Go?✗ Incorrect
i-- subtracts 1 from i.
Can you write
++i in Go?✗ Incorrect
Go only supports postfix increment and decrement operators.
Which of the following is a correct way to increment
count in Go?✗ Incorrect
You can increment by 1 using either count = count + 1 or count++.
Explain how increment and decrement operators work in Go and how they differ from other languages.
Think about how you write <code>i++</code> and if you can use it inside other expressions.
You got /3 concepts.
Write a short Go code snippet that declares a variable and increments it twice, then prints the result.
Use <code>var</code> or :=, then <code>++</code>, then <code>fmt.Println</code>.
You got /3 concepts.