Conditionals

A conditional statement is one that only runs when certain conditions are met. The primary entry point is the if statement, which takes the form of 'if this is true, do this'.

if :x = 5 [
    print 5
]

;this will print 5 only when the variable x equals 5

The ifelse block takes three arguments: the condition and two command blocks. The first command block will run if the condition evaluates to true, the second block will run if the condition evaluated to false. The following example will evaluate to false, because 1 never equals 0, and will print "false" as a result:

ifelse 1 = 0 [
    print "true
] [
    print "false
]

There is no while loop in JSLogo, but the same logic can be used with a loop and the break function. This will end a loop when run.

loop [
    print 'running'
    if :condition = true [
        break
    ]
]

Logical Operators

JSLogo uses a number of mathematical and logical operators that can be used in this context.

Function NameInputDescription
=value1 = value2Returns true if the values are equal, false otherwise
!=value1 != value2Returns true if the values are not equal, false otherwise
>value1 > value2Returns true if value1 is greater than value2
>value1 < value2Returns true if value1 is less than value2
<=value1 <= value2Returns true if value1 is less than or equal to value2
>=value1 >= value2Returns true if value1 is greater than or equal to value2
xorxor (condition1) (condition2)Returns true if either condition1 or condition2 are true, but not if both are true
notnot conditionReturns true if condition is false, false if condition is true
oror (condition1) (condition2)Returns true if either condition is true
andand (condition1) (condition2)Returns true if both conditions are true
Last updated on December 21, 2022