Menu
Coddy logo textTech

Comparison Operators - Part 1

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

Comparison operators compare two values and return a boolean result (true or false).

The equality operator (==) checks if two values are equal:

a := 5
b := 5
result := a == b
fmt.Println(result)

This outputs:

true

The inequality operator (!=) checks if two values are not equal:

x := 10
y := 20
notEqual := x != y
fmt.Println(notEqual)

This outputs:

true
challenge icon

Challenge

Beginner

In this challenge, you'll practice using the equality (==) and inequality (!=) comparison operators in Go.

We have two variables with predefined values. Your task is to compare these variables using both operators and store the results in the provided boolean variables.

Then we'll print the results to see if your comparisons are correct.

Cheat sheet

Comparison operators compare two values and return a boolean result (true or false).

The equality operator (==) checks if two values are equal:

a := 5
b := 5
result := a == b
fmt.Println(result) // true

The inequality operator (!=) checks if two values are not equal:

x := 10
y := 20
notEqual := x != y
fmt.Println(notEqual) // true

Try it yourself

package main

import "fmt"

func main() {
	// Predefined variables to compare
	num1 := 10
	num2 := 10
	name1 := "Go"
	name2 := "Python"
	
	// TODO: Compare num1 and num2 using == (equality) operator
	// and store the result in numbersEqual
	numbersEqual := false // Replace this line
	
	// TODO: Compare name1 and name2 using != (inequality) operator
	// and store the result in namesNotEqual
	namesNotEqual := false // Replace this line
	
	// Print the results
	fmt.Println("Are the numbers equal?", numbersEqual)
	fmt.Println("Are the names different?", namesNotEqual)
}
quiz iconTest yourself

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

All lessons in Fundamentals