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? trueThe NOT operator inverted isEven (which was false for 7) to true, correctly identifying that 7 is odd.
Challenge
BeginnerIn 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? trueThe 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)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions