Menu
Coddy logo textTech

Logical NOT Operator

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

The logical NOT operator (!) reverses a boolean value. It turns true into false and false into true.

Create a program that uses the NOT operator to check if a number is not even:

num := 7
isEven := num%2 == 0
isOdd := !isEven
fmt.Println("Is", num, "odd?", isOdd)

This outputs:

Is 7 odd? true

The NOT operator inverted isEven (which was false for 7) to true, correctly identifying that 7 is odd.

challenge icon

Challenge

Beginner

In this challenge, you'll practice using the logical NOT operator (!) in Go.

The logical NOT operator inverts a boolean value: true becomes false and false becomes true.

We've provided a boolean variable isRaining. Your task is to use the NOT operator to invert its value and store the result in the result variable.

Cheat sheet

The logical NOT operator (!) reverses a boolean value. It turns true into false and false into true.

num := 7
isEven := num%2 == 0
isOdd := !isEven
fmt.Println("Is", num, "odd?", isOdd)

This outputs:

Is 7 odd? true

The NOT operator inverted isEven (which was false for 7) to true, correctly identifying that 7 is odd.

Try it yourself

package main

import "fmt"

func main() {
	// Boolean variable
	isRaining := true
	
	// TODO: Use the NOT operator (!) to invert the value of isRaining
	// and store the result in the variable below
	result := isRaining
	
	// Print the original value and the inverted value
	fmt.Println("Original value:", isRaining)
	fmt.Println("After using NOT operator:", result)
}
quiz iconTest yourself

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

All lessons in Fundamentals