Menu
CoddyTech

Comparison Operators

Part of the Fundamentals section of Coddy's GDScript journey. Lesson 20 of 92.

Comparison operators compare two values. The result is a bool: true or false.

  • == equal: 1 == 2 is false
  • != not equal: 1 != 2 is true
  • > greater than: 1 > 2 is false
  • < less than: 1 < 2 is true
  • >= greater than or equal: 2 >= 2 is true
  • <= less than or equal: 3 <= 2 is false

You can store the result in a variable:

var hp := 13
var max_hp := 12
var too_high := hp > max_hp # true

= stores a value in a variable, while == compares two values.

An integer and a float with the same value are equal: 5 == 5.0 is true. A number and a string cannot be compared: GDScript reports 5 == "5" as an error before the program runs.

Strings can be compared with == and !=: "sword" == "sword" is true.

challenge icon

Challenge

Easy

Declare two variables n1 and n2 with the values 8 and 9.

Then declare n3 that holds whether n1 is greater than n2.

The print() line is already there and outputs:

n1 = 8, n2 = 9, n3 = false

Try it yourself

extends Node

func _ready():
	# Declare n1, n2 and n3 below

	# Don't change the line below
	print("n1 = ", n1, ", n2 = ", n2, ", n3 = ", n3)
quiz iconTest yourself

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

All lessons in Fundamentals

Practice on your own: Online GDScript compiler