2

I need to check a variable if it is even or odd and I don't know how to do it in Imagine Logo.

In case that it is not possible, I need at least something like this...

if (a = 1 || a = 2 || a = 3)
TrotlOtl
  • 103
  • 1
  • 1
  • 9

1 Answers1

2

Typically this is done by using something that finds the remainder after a division. Even numbers divided by 2 will have a remainder of 0. Odd numbers divided by 2 will have a remainder of 1. If your numbers are negative you might have to pay attention to how the remainder is given back.

I don't know about Imagine Logo specifically, but this online interpreter for "a Logo" has three ways to say it (under the "Reference" link):

remainder expr expr
expr % expr
modulo expr expr

Outputs the remainder (modulus). For remainder and % the result has the same sign as the first input; for modulo the result has the same sign as a the second input.

Assuming you have these available, then if you want to test if negative numbers are odd or even, it will be easier if you use remainder or % instead of modulo. So for even:

(a % 2) = 0
(remainder a 2) = 0

And for odd:

(a % 2) = 1
(remainder a 2) = 1

You may or may not want to look at the Wikipedia page for "Modulo operation".