Menu
Coddy logo textTech

Comparison Operators - Part 2

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

Go provides additional comparison operators for comparing numerical values.

Use the greater than operator (>) to check if one value is larger than another:

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

This outputs:

true

Other comparison operators include less than (<), greater than or equal to (>=), and less than or equal to (<=):

x := 7
y := 7
fmt.Println(x < y)  // false
fmt.Println(x >= y) // true
fmt.Println(x <= y) // true
challenge icon

Challenge

Beginner

In this challenge, you'll practice using comparison operators in Go. We have defined several variables with different values. Your task is to complete the missing comparison expressions to make the program print the expected output.

You need to use the following comparison operators: < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

Cheat sheet

Go provides comparison operators for numerical values:

Greater than (>):

a := 10
b := 5
result := a > b  // true

Other comparison operators:

  • < - less than
  • >= - greater than or equal to
  • <= - less than or equal to
x := 7
y := 7
fmt.Println(x < y)  // false
fmt.Println(x >= y) // true
fmt.Println(x <= y) // true

Try it yourself

package main

import "fmt"

func main() {
	// Some predefined variables
	age1 := 25
	age2 := 30
	age3 := 25
	
	// TODO: Complete these comparison expressions using <, >, <=, or >=
	isAge1LessThanAge2 := // Check if age1 is less than age2
	isAge2GreaterThanAge1 := // Check if age2 is greater than age1
	isAge1LessThanOrEqualToAge3 := // Check if age1 is less than or equal to age3
	isAge2GreaterThanOrEqualToAge3 := // Check if age2 is greater than or equal to age3
	
	// Print the results
	fmt.Println("Is age1 less than age2?", isAge1LessThanAge2)
	fmt.Println("Is age2 greater than age1?", isAge2GreaterThanAge1)
	fmt.Println("Is age1 less than or equal to age3?", isAge1LessThanOrEqualToAge3)
	fmt.Println("Is age2 greater than or equal to age3?", isAge2GreaterThanOrEqualToAge3)
}
quiz iconTest yourself

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

All lessons in Fundamentals