Jump to content

Rexx Programming/How to Rexx/boolean

From Wikibooks, open books for an open world

Boolean values are logical truth values that are considered either true or false. Rexx uses 1 to mean "true" and 0 to mean "false". Some programming languages have many different kinds of values that could be treated as Boolean, depending on the context. The Rexx programming language itself only recognizes 0 and 1 for this purpose.

/* Some Boolean variables */
raining = 1
freezing = 1
snowing = 0
hailing = 0
/* Boolean values affect control structures... */
/* The computer will say we might need an umbrella. */
select
 when raining then say "You might need an umbrella."
 when snowing then say "Let's build a snowman!"
 when hailing then say "Ouch! There's hail."
 otherwise say "There might not be any precipitation."
end
/* The computer will tell us to watch out for ice. */
if freezing then say "Watch out for ice!"
else say "At least it's not freezing outside."
/* This loop will never start because it isn't snowing. */
do while snowing
 say "It'll just keep snowing forever!"
end

Logical Operators

[edit | edit source]

Operators exist to combine Boolean values into new Boolean values. The ampersand (&) means "and", the pipe symbol (|) means "or" and the backslash (\) means "not".

if raining | snowing then
 say "It's either raining or snowing--maybe both."
if raining & freezing then
 say "Expect freezing rain."
if \ snowing then
 say "No snow today."
if \ (raining | snowing) then
 say "It's neither raining nor snowing."

Relational Operators

[edit | edit source]

Some other operations combine other kinds of values to make true or false statements. They will always result in 0 or 1. You can read about them in the comparative operators section. The remainder when an integer is divided by 2 will technically result in a Boolean value, but other kinds of remainders will not.

say 2 < 3             /* says 1 */
say "happy" = "sad"   /* says 0 */
/* We can use Booleans to validate input: */
do until positive & even
 say "Enter a positive even number:"
 pull number
 positive = number > 0
 odd = number // 2
 even = \ odd
end