跳转到内容

Rexx 编程/Rexx 指南/布尔值

来自维基教科书,自由的教科书

布尔值是逻辑真值,被认为是真或假。Rexx 使用 1 代表“真”,使用 0 代表“假”。一些编程语言有许多不同类型的值,这些值在不同的上下文中可以被视为布尔值。Rexx 编程语言本身只识别 0 和 1 用于此目的。

/* 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

逻辑运算符

[编辑 | 编辑源代码]

运算符用来将布尔值组合成新的布尔值。按位与 (&) 表示“与”,管道符号 (|) 表示“或”,反斜杠 (\) 表示“非”。

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."

关系运算符

[编辑 | 编辑源代码]

一些其他操作将其他类型的值组合起来以生成真假语句。它们始终会产生 0 或 1。您可以在比较运算符部分了解有关这些运算符的信息。整数除以 2 后的余数在技术上会产生一个布尔值,但其他类型的余数则不会。

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
华夏公益教科书