Jump to content





Learning Action! part 3, loop da loop

Posted by hloberg, in programming, Atari 800, Action! 21 January 2019 · 68 views

action! atari ataro 8-bit atari 800 programming

Today’s lesson is loops.
Today’s lesson is loops.
Today’s lesson is loops.
Today’s... sorry, couldn’t resist the joke.
 
Welcome back to learning about OSS Action! for the Atari 8-bit.
As usual, We’ll start with a program,
 
 
PROC MAIN()

 

;part 1
  BYTE V
  PRINTE(" ")
  PRINT ("ENTER A NUMBER: ")
  V=INPUTB() ;INPUT BYTE

 

  IF 5>V THEN 
    PRINT("5>")
    PRINTBE(V) ;PRINT BYTE WITH C/R
  ELSE
    PRINTB(V)
    PRINTE(">5") ;PRINT WITH C/R
  FI  ; END OF IF
;part 2
WHILE V>0
   
  DO
    V==-1 ; decrement counter
    PRINTB(V)
    PRINT(" ")
  OD
                             
RETURN
 
 
Last time we looked at part 1 of this program, the IF THEN statement.
This time, part 2, we are going to look at loops in Action!
 
 
I haven’t mentioned it before but it is a good idea to type the programs in Action! and see what they do before the lessons. This will assist in the understanding of the code.
 
 
Action! takes loops seriously. You can’t have good structured code without loops (Otherwise you might be tempted to use, horrors!, a GOTO). That’s why they have three types of loops; FOR, WHILE and UNTIL.
 
 
Every good BASIC programmer knows FOR..TO..STEP..NEXT loops.
I still remember my first program, a silly FOR...NEXT loop written in TI BASIC.
5 REM ADVENTURE @ PO-FOLKS
10 FOR I=1 TO 100
20 PRINT “MORE TEA?”
30 NEXT I
Believe me, it was funny at the time.
 
To do a similar loop in Action! you would write thus:
PROC MAIN()
  BYTE I ;declare var
  FOR I=1 TO 100 ;condition for loop
  DO ;this begins the loop, all types
    PRINTE(“MORE TEA?”)
  OD ;this ends the loop, all type
RETURN
 
All Action! loops are contained between a DO and a OD. Doesn’t matter if it’s a FOR, WHILE or UNTIL. You could actually omit the condition all together and just have the DO ..... OD and it would still work.
 
;Example of DO...OD with no condition
PROC MAIN()
  DO
    PRINTE(“MORE TEA?”)
  OD
RETURN
 
But you would have one infinite loop (just like Apple’s address: 1 Infinite loop, Cupertino Ca., ha).
Without a condition (FOR, WHILE, UNTIL) the DO doesn’t know when to stop and just goes on and on and you would soon have the entire planet covered in Tea. You wouldn’t want that would you? That’s why the conditions.
 
 
A quick aside.
Before we move on I want to get this, abomination of bad programming out of the way, the EXIT statement. You can jump out of a loop by using EXIT. Lets use the above program as an example.
 
PROC MAIN()
  BYTE  A
  A=0
  DO
    PRINTE(“MORE TEA?”)

 

    A=A+1
  
    IF A=100 THEN
      EXIT
    FI
  OD
 
RETURN ;exit jumps here
 
  
The EXIT command, when executed, puts you in the next command after the OD, in this case the RETURN exiting the program.
As I said, EXIT works but it’s bad programming and makes the program had to follow. It’s like the POP command as it does clear the stack but, don't do it, really, just, don’t. Using a complete FOR, WHILE or UNTIL is much more clear.
 
 
Back to FOR...TO...NEXT.
FOR in Action! works like FOR in BASIC.
FOR [variable counter]=[intial count] TO [ending count] STEP [increments].
Nothing new here. Depending on you need you can use BYTE, CARD or INT as the variable counter.
 
 
 
WHILE and UNTIL.
For Atari BASIC (and most 8-bit BASICS of the time) WHILE and UNTIL are new. You can find WHILE and UNTIL in Pascal, ‘C’ an other structured programming languages (WHILE has even found it’s way in BASIC XL the sneaky beast).
WHILE is what is called a pre-condition test loop. What that means is that you test your condition before the loop is executed.
 
Example:
PROC MAIN()
A=0
 
WHILE A>1
 
DO
 
    A=A+1
    PRINTE(“MORE TEA”)
 
OD
 
RETURN
 
You would think you would have another infinite loop, but this loop never executes. Why? Because A=0, less than 1. WHILE test the condition 1st thing. WHILE sees that A=0 so the test fails and the loop is bypassed.  No tea for you.
 
WHILE has a place when you have a need for a loop that you may not ever want to execute once. Here is how you would do the same with IN BASIC for reference.
 
10 A=0
20 IF A=0 THEN 100
30 A=A+1
40 PRINT “MORE TEA”
50 GOTO 30
100 REM BYPASS FOR LOOP
 
Same number of lines but the WHILE is more clear about what’s going on.
 
 
The last type of loop is the UNTIL. It’s very much like WHILE but is a post-condition check which means, it will always execute once.
Example:
PROC MAIN()
A=0
DO
    A=A+1
    PRINTE(“MORE TEA”)
UNTIL A>2
OD
RETURN
In this case the A=0 means nothing except initializing the variable to 0. The loop will always drop down once to the UNTIL where the condition is checked. Here the loop will execute 3 times since the counter A=A+1 is before the UNTIL A>2.
Note that the UNTIL is in the DO...OD loop not outside like FOR...NEXT and WHILE.
For all intents and purposes UNTIL does the same as
IF A>2 THEN
  EXIT
FI
command shortened and clearer.
 
One other command from the original program I want to discuss is V==-1.  This is a shorthand counter. I am decrementing the counter here. V==-1 is the same as V=V-1.
V==+1 would be V=V+1, V==+15 would be the same as V=V+15 etc...
If you know C this can be a bit confusing as == in C is conditional equivalence and -- and ++ would be the counter. Hopefully you don’t know C and confused by this. Ignorance is bliss.
 
That’s it for this time. I’m still thinking on what I’m going to do for the next lesson so I’ll be as surprised as you.
Till then, HLO