A.6 Testing an expression with the primitive if

As in all programming languages, Logo allows you to check if a condition is satisfied and then to execute the desired code if it’s true or false.

With the primitive if you can realize those tests.



if expression_test list1 list2



if expression_test is true, the instructions included in list1 are executed. Else, if expression_test is false, the instructions in list2 are executed. This second list is optional.



Examples:

Important: When the result of the first predicate is equal to false, the primitive if looks for a second list, I mean an expression starting with a square bracket. In some very rare cases, it can’t be done, and you’ll have to use the primitive ifelse . For example:

 # We  affect two lists in variables a and b
  make "a [print true]
  make "b [print false]
 
 # First test with the primitive if--> The second list can’t be evaluated.
  if 1=2 :a :b
  What to do with [print false]?
 
 # Second test with the primitive ifelse --> success.
  ifelse 1=2 :a :b
  false