Menu
CoddyTech

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 + 2 is 5
  • - subtraction: 3 - 2 is 1
  • * multiplication: 3 * 2 is 6
  • / division: 4 / 2 is 2
  • ** power: 2 ** 3 is 8 (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 8

As in math, *, / and ** run before + and -. Parentheses change the order:

print(3 + 2 * 4) # 11
print((3 + 2) * 4) # 20

When one of the values is a float, the result is a float too:

var price := 2.5
print(price * 3) # 7.5
challenge icon

Challenge

Easy

Declare three variables:

  • a with the value 5.2
  • b with the value 2.6
  • c that holds the result of a / b

Write your declarations above the comment. The print() line is already there and outputs:

a = 5.2, b = 2.6, c = 2.0

Try 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)
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