0
0
Goprogramming~5 mins

Increment and decrement in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ADivides i by 2
BSubtracts 1 from i
CAdds 1 to i
DMultiplies i by 2
Which of these is valid in Go?
Ai++
Bj = i++
C++i
Dj = ++i
What happens if you write i-- in Go?
ASyntax error
Bi increases by 1
Ci is set to zero
Di decreases by 1
Can you write ++i in Go?
ANo, Go only supports postfix <code>i++</code>
BYes, it works like in C
CYes, but only inside loops
DNo, Go does not support increment operators
Which of the following is a correct way to increment count in Go?
Acount = count + 1
BBoth A and B
Ccount++
Dcount += 2
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.