Amiga World Official AmigaDOS 2 Companion - eBook-ENG

Page 339

Introduction to ARexx

321

do i = 1 to number by 1 say score, i

end say 'The average was 'total / number)

In this example, you enter the scores into a group of compound symbols that share the score, stem. This sets up an array of variables that you can access using the stem plus a tail that identifies the location of the variable within the array. The do loop is a natural way to step through such an array, letting you access all the variables while using just one name. The second part of this program is a bubble sort. It will sort any numeric or string array. Every time through the inner loop, it "bubbles" the highest value left in the unsorted part of the array to the current score.i variable. The iterate in the comparison instruction jumps the loop to the end statement (if score.i is greater than or equal to score.j, they are already in the correct order). The bubble sort is not the most efficient sorting algorithm, but it gets the job done. In many cases, you can't predict how many times you will be going through a loop. The do instruction combines with two others, while and until, to handle these indefinite situations. The while option goes like this: /* Do-While Loop */ response = Y

do while response = 'Y' say 'enter a number' pull number say 'The square of 'number ' is 'number ** 2 say 'Do you wish to square another number (Y/N)?' pull response end

The until option looks similar. /* Do-Until */ tries = 0

do until flag = 1 say 'What is the product of 6 X 4' pull response

if response = 24 then flag = 1 else say 'Incorrect, try again.1 tries = tries + 1

end if tries = 1 then do


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.