Select |
Flow control | |
Aptilis 1 |
Select
select is a very useful statement (not a sub) which will execute one block
of lines out of several other blocks depending on the value of a variable.
select is a more practical way to implement such things as:
if a = 1 ... end if if a = 2 ... end if if a = 3 ... end if if a = 4 ... end if etc...select will use a variable to decide of what action to take, but first, it will round the variable to its integer value. The corollary to that is that the different possible actions will reflect different cases attached to integer values only.
Example 1:
A number speller
rem the user has to enter a number n = input(2) select n case 1 print("one") break case 2 print("two") break case 3 print("three") break default print("errrrm, I don't know!") end selectResult:
(The user entered 2) two (The user entered 3) three (The user entered 5) errrrm, I don't know! |
Example 2:
A lazy number speller
// the user has to enter a number n = input(2) select n case 1 case 2 case 3 print("one or two or three") break case 4 print("four or") case 5 print("five") break default print("errrrm, I don't know!") end selectResult:
(The user entered 2) one or two or three (The user entered 3) one or two or three (The user entered 5) five (The user entered 4) four or five |