Arithmetic Operators
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 15 of 92.
Operators perform operations on values. The arithmetic operators do math:
+addition:3 + 2is5-subtraction:3 - 2is1*multiplication:3 * 2is6/division:4 / 2is2**power:2 ** 3is8(2 * 2 * 2)
Operators work with values and with variables. You can store the result in a new variable:
var a := 3
var b := 5
var c := a + b # c holds 8As in math, *, / and ** run before + and -. Parentheses change the order:
print(3 + 2 * 4) # 11
print((3 + 2) * 4) # 20When one of the values is a float, the result is a float too:
var price := 2.5
print(price * 3) # 7.5Challenge
EasyDeclare three variables:
awith the value5.2bwith the value2.6cthat holds the result ofa / b
Write your declarations above the comment. The print() line is already there and outputs:
a = 5.2, b = 2.6, c = 2.0Try it yourself
extends Node
func _ready():
# Declare a, b and c below
# Don't change the line below
print("a = ", a, ", b = ", b, ", c = ", c)
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