Jump to content

Rexx Programming/How to Rexx/control structure

From Wikibooks, open books for an open world

Control structures (also known as control constructs) are constructs used within a program to control the flow of a program based on given conditions. Control structure typically uses conditional branching instructions or loop control statements. Code within control structures is typically arranged into blocks in a similar fashion to control structures written in C.

Conditional Branching

[edit | edit source]

The following example shows a conditional code being used as a conditional branch:

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

Here is a conditional branch with two parts:

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

As mentioned earlier, we can use blocks so that more than one thing can happen if a condition is met (or unmet).

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

Select

[edit | edit source]

Rexx also has a construct for choosing the first true condition from multiple options.

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

Loops

[edit | edit source]

The following example shows a block of code being used in a loop:

do number = 1 to 10
  say number
end