Menu
CoddyTech

Logical Operators Part 2

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

A truth table lists the result of a logical operator for every combination of values.

and is true only when both values are true:

  • false and false is false
  • false and true is false
  • true and false is false
  • true and true is true

or is true when at least one value is true:

  • false or false is false
  • false or true is true
  • true or false is true
  • true or true is true

not takes one value and reverses it:

  • not true is false
  • not false is true

For example, a player who carries a bow but no sword:

var has_sword := false
var has_bow := true
print(has_sword or has_bow) # true
print(has_sword and has_bow) # false
print(not has_sword) # true
challenge icon

Challenge

Easy

Change the values of b1 and b2 to whole numbers so that b3 is true.

b3 is true when the product of b1 and b2 is greater than their sum and the two numbers are not equal.

Do not change the line of b3 or the print() line. The output must be:

b3 = true

Try it yourself

extends Node

func _ready():
	# Change the values of b1 and b2
	var b1: int = 0
	var b2: int = 0

	# Don't change the lines below
	var b3: bool = (b1 * b2 > b1 + b2) and not (b1 == b2)
	print("b3 = ", b3)
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