Introduction
Increment and decrement help you add or subtract 1 from a number easily.
Counting items in a list one by one.
Moving forward or backward in a loop.
Tracking how many times something happens.
Adjusting a score or value step by step.
Increment and decrement help you add or subtract 1 from a number easily.
variable++ // adds 1 to variable variable-- // subtracts 1 from variable
These operators only add or subtract 1.
They work only with variables, not with constants or expressions.
count := 5 count++ // count becomes 6
score := 10 score-- // score becomes 9
i := 0 for i < 3 { fmt.Println(i) i++ }
This program shows how to increase and then decrease a number by 1.
package main import "fmt" func main() { x := 3 fmt.Println("Start:", x) x++ fmt.Println("After increment:", x) x-- fmt.Println("After decrement:", x) }
Increment (++) and decrement (--) operators are statements in Go, not expressions.
You cannot use them inside other expressions like y = x++.
Use variable++ to add 1.
Use variable-- to subtract 1.
They help count or move step by step in your code.