跳轉至內容

Rexx 程式設計/Rexx 指南/控制結構

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

控制結構(也稱為控制構造)是在程式中用來基於給定條件控制程式流程的構造。控制結構通常使用條件分支指令或迴圈控制語句。控制結構中的程式碼通常以類似於 C 語言中控制結構的方式被組織成程式碼塊。

條件分支

[編輯 | 編輯原始碼]

以下示例展示了條件程式碼作為條件分支使用的情況。

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

這是一個包含兩個部分的條件分支。

if guess = 6 then
  say "Wow! That was a lucky guess!"
else
  say "Sorry, the number was actually 6."

如前所述,我們可以使用程式碼塊,以便在條件滿足(或不滿足)時執行多個操作。

if score >= 100 then
 do
  say "Congratulations! You won."
  say "Go ahead and enter your initials below for our high score list."
  initials = LineIn()
 end
else
 do
  say "Let's keep playing!"
  say "What do want your next move to be?"
  next_move = LineIn()
 end

Rexx 還提供了一種構造,用於從多個選項中選擇第一個為真的條件。

select
  when age < 3 then
    say "You are a toddler."
  when age < 12 then
    say "You are a child."
  when age < 20 then
    say "You are a teenager."
  when age >= 65 then
    say "You are are a senior."
  when age >= 40 then
    say "You are middle-aged."
  otherwise
    say "You are a young adult."
 end

以下示例展示了程式碼塊在迴圈中使用的情況。

do number = 1 to 10
  say number
end
華夏公益教科書