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 == 2isfalse!=not equal:1 != 2istrue>greater than:1 > 2isfalse<less than:1 < 2istrue>=greater than or equal:2 >= 2istrue<=less than or equal:3 <= 2isfalse
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
EasyDeclare 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 = falseTry 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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler