跳轉到內容

Rexx 程式設計/Rexx 指南/if

來自華夏公益教科書,開放的書籍,開放的世界

if 條件結構允許根據布林表示式的結果有條件地執行 語句程式碼塊。只有當表示式計算結果為真值時,條件中的分支才會執行。if 條件最簡單的形式是

if CONDITION then STATEMENT

[編輯 | 編輯原始碼]

例如

if guess = 6 then
  say "Wow! That was a lucky guess."

沒有 endif 組成部分

[編輯 | 編輯原始碼]

請注意,Rexx 程式語言語法不使用 endif 作為語法組成部分。下面示例中放置在註釋中的 endif 僅用於美觀目的,以幫助縮排。這些註釋會被 Rexx 直譯器忽略。

if guess = 6 then
  say "Wow! That was a lucky guess."
else
  say "Sorry! That was not the right number."
/* endif */


使用 do 結構有條件地執行包含多個語句的分支

[編輯 | 編輯原始碼]

為了有條件地在條件分支內執行多個語句,有必要將這些語句包含在一個 do 和 end 塊中。

if guess = 6 then
  do
    say "Wow! That was a lucky guess."    /* Multiple statements in a do block */
    prize = 30000
  end

巢狀條件結構

[編輯 | 編輯原始碼]

可以建立巢狀的 if 結構。

if age > 79 then
  say "Wow! You are so old that you are almost antique!"
else
  if age >=65 then
    say "You are a pensioner"
  else
    if age > 21 then
      say "You have the key to the door"
    else
      say "You are just a youngster"
    /* endif */
  /* endif */
/* endif */
華夏公益教科書