Menu
Coddy logo textTech

Increment and Decrement

Part of the Fundamentals section of Coddy's GO journey — lesson 20 of 109.

Increment and decrement operators provide a shorthand way to add or subtract 1 from a variable.

Go uses ++ to increment and -- to decrement a variable by 1:

count := 5
count++  // Same as: count = count + 1
fmt.Println(count)

count--  // Same as: count = count - 1
fmt.Println(count)

Output:

6
5

Important: In Go, ++ and -- are statements, not expressions. They can only be used as standalone lines, not within other expressions.

challenge icon

Challenge

Beginner

In this challenge, you'll practice using increment (++) and decrement (--) operators in Go.

We have a variable counter with an initial value. Your task is to:

  1. Increment the counter twice using the ++ operator
  2. Decrement the counter once using the -- operator

The program will then print the final value of the counter.

Cheat sheet

Go uses ++ to increment and -- to decrement a variable by 1:

count := 5
count++  // Same as: count = count + 1
count--  // Same as: count = count - 1

Important: In Go, ++ and -- are statements, not expressions. They can only be used as standalone lines, not within other expressions.

Try it yourself

package main

import "fmt"

func main() {
	// Starting value for our counter
	counter := 5
	
	// TODO: Increment counter twice using the ++ operator
	
	// TODO: Decrement counter once using the -- operator
	
	// Print the final value
	fmt.Println("Final counter value:", counter)
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals