In this post I explore the conditionals provided by the Action! language on the Atari 8 bit computer.
Two of the conditionals have already been touched on, WHILE and UNTIL. They are primarily use for loop control. The remaining conditional is the IF statement. It has the basic form of “IF condition THEN action”.
A condition is a test and the test must evaluate to either true or false. If the condition is true, the commands (statements) following the THEN clause are executed. If the condition is false, the next branch of the IF statement is executed (tested). An IF statement can consist of a single condition or multiple condition tests. After the initial IF, secondary conditions are added with ELSEIF. Each successive condition will only be evaluated if the previous tests have failed. And as seen below a default condition (ELSE) if none of the previous conditions evaluate to true. Once a condition passes and the code for that condition is executed, program flow jumps to the end of the conditional block (FI). The syntax is:
IF condition then ; Some commands ELSEIF condition then ; Some commands ELSE ; Some commands FI
A condition is constructed of an expression using one of several different mathematical operators. Conditions can also be compounded by combining two expressions together with AND, or OR. With AND, both expressions must be true for the commands following the THEN clause to execute. With OR, only one of the expressions must be true for the commands following the THEN clause to execute. Operators are:
- > : Greater Than
- >= : Greater Than or Equal To
- < : Less Than
- <= : Less Than or Equal To
- = : Equal To
- # : Not Equal To. This can also be expressed as “<>”.
Either side of the condition could be a complex expression such as “(var * 3 MOD 2)”. They don’t have to be simple, though simple ones are easier to read and debug.
Building a Condition
To test if variable x is >= 25 you would write:
if x>=25 then
To test if variable x is >= 25 and y < 10 you would write:
if x>=25 and y<10 then
Sample Program Breakdown
As usual I created a program to demonstrate. Here I break it down.
This is the start of the program and start of the main procedure. It also initializes a BYTE variable to value 0:
MODULE PROC Main() BYTE bIn=[0]
This section initializes the screen display:
; Setup screen Poke(82,0) Graphics(0) PutE() PrintE("-[Conditionals]-------------------------")
Here I start a loop. There is no condition assigned to it via FOR, WHILE, or UNTIL, so it will loop forever:
DO
This prints a prompt and gets a byte from the user. The input byte is stored in bIn:
; Get value from user Print("Value (0-100)?") bIn=InputB()
Now the start of the conditional test block. The first test checks to see if the value input is less than 25. If bIn is less than 25, then “Value is between 0 and 24.” is printed:
; Test value if bIn < 25 then PrintE("Value is between 0 and 24.")
The checks could end here by placing an “fi” to end the testing block. Instead I now apply a successive check. This one is a compound conditional. It checks if bIn is greater than or equal to 25 and if bIn is less than 50. If both conditions pass, then “Value is between 25 and 49.” is printed:
elseif bIn >= 25 and bIn <50 then PrintE("Value is between 25 and 49.")
Next I applied another successive conditional check. This is another compound conditional. It checks if bIn is greater than or equal to 50 and if bIn is less than 75. If both conditions pass, then “Value is between 50 and 74.” is printed:
elseif bIn >= 50 and bIn <75 then PrintE("Value is between 50 and 74.")
Next I applied another successive conditional check. This is another compound conditional. It checks if bIn is greater than or equal to 75 and if bIn is less than 100. If both conditions pass, then “Value is between 75 and 99.” is printed:
elseif bIn >= 75 and bIn <100 then PrintE("Value is between 75 and 99.")
Now I apply a default condition. This will execute only if all other conditions fail. For this example program, it will execute if the value of bIn is 100 or more. If it is, then “Value is 100 or greater!” is printed:
else PrintE("Value is 100 or greater!")
This statement closes the conditional test block.
fi
This statement just puts an empty line on the screen to separate the text for the next loop iteration:
PutE()
This completes the main program loop. Because no conditions are applied to it, it is an infinite loop and will continue forever:
OD
This marks the end of the program, but will never be reached because of the infinite loop above:
RETURN
Complete Source
MODULE PROC Main() BYTE bIn=[0] ; Setup screen Poke(82,0) Graphics(0) PutE() PrintE("-[Conditionals]-------------------------") DO ; Get value from user Print("Value (0-100)?") bIn=InputB() ; Test value if bIn < 25 then PrintE("Value is between 0 and 24.") elseif bIn >= 25 and bIn <50 then PrintE("Value is between 25 and 49.") elseif bIn >= 50 and bIn <75 then PrintE("Value is between 50 and 74.") elseif bIn >= 75 and bIn < 100 then PrintE("Value is between 75 and 100.") else PrintE("Value is 100 or greater!") fi PutE() OD RETURN
Results
Here I’ve entered 5 values testing each of the conditions:
Next I will create a useful program using what I’ve learned so far.