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