Jump to content





Learning Action! Part 3; If Then

Posted by hloberg, in programming, Atari 800, Action! 16 January 2019 · 117 views

action! Tutorial Atari 8-bit Atari programming

Welcome Back to Action!
I'm going to break this lesson up into two parts. Today, the first part we will look at IF-THEN statements.
Next time we’ll look at LOOPs.
So here’s the program.
 
;(PART 1)
PROC MAIN()
 
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
 
;(PART 2)
 
WHILE V>0
  
  DO
  V==-1
    PRINTB(V)
    PRINT(" ")
 
OD
                            
RETURN
 
Now lets look at the program line by line.
 
PROC MAIN() ; this is a review. this is the name of the procedure that all the program goes under. Every program in ACTION! is encased in a procedure. Be it called MAIN() or whatever. And every procedure must end with a RETURN. 
 
  BYTE V ; all variables must be declared before usage. this declares V as a BYTE type variable.
 
PRINTE(" ") ; print blank space with End of Line/Carriage  Return. That's what the E in PRINTE means.
PRINT ("ENTER A NUMBER: ") ;No extender beyond PRINT so just PRINT the stuff in quotes. No EOL.
 
V=INPUTB() ;INPUT BYTE ; Since no EOL on previous line the INPUT is attached to the end of the above print.
  INPUTB() accepts the screen input. The B in INPUTB means accept only byte input. The INPUT command has to match the type of input variable where V is a byte type. The () at the end are not optional, this is part of most of the commands actually being Procedures.
This is all review.
 
 
And now for something new IF-THEN
 
IF 5>V THEN
 
  ;(Stuff goes here when 5>V, as many lines as you need)
 
  ELSE
 
;(otherwise do these lines. again multi-line)
 
  FI ; (close off IF statement)
 
As you can see you can put many lines in an IF statement. This is very helpful in structuring your program to be readable. Just be sure to end the IF...Then with the command FI (which is IF backwards).
You do need a FI even if its just one line. You have to close off the IF statement or the compiler thinks everything after is still part of the IF..THEN statement.
You don't have to have an ELSE, it is optional. ELSE works just like ELSE in BASIC,
 
For clarity I would always put the IF..THEN command and the execution on separate lines:
 
;(DON”T DO THIS!)
If A>B THEN Print(“this”) FI
 
; (DO THIS INSTEAD)
IF A>B THEN
  Print(“this”)
FI
 
Both compile but the 2nd one is much easier to see what it does when quickly scanning through a program.
 
 
The relational operators are all the same as BASIC except one additional.
> greater than
< less than
= equal to
>= greater or equal
<= less or equal
<> and # both mean -not equal
AND - logical AND
OR - logical or
(...) parenthesis can also be used to change the order of the mathematical operation.
 
Be careful to compare only like types; BYTE to BYTE, INTEGER to INTEGER and such. Strange things will happen if you mix types.
Ex:
IF (BYTE-variable > INTeger-variable) THEN
  (do something)
FI
I would shy away from this.
That isn’t to say it wouldn’t work, it will. Still, the outcome may not always be what you expect.  
 
Another command that can be added to the IF ... THEN is ELSEIF.
 
It works like this.
 
IF (A=1) THEN
  PRINTE(“A=1”)
 
ELSEIF A=2 THEN
  PRINTE(“A=2”)
 
ELSEIF A=3 THEN
  PRINTE(“A=3”)
 
ELSE
  PRINTE(“A is not 1, 2 or 3”)
 
FI
 
ELSEIF enables you to specify several commands for a set of related conditions.
Variable A can be tested for, and have a subsequent outcome for, 1, 2, 3 or (with the ELSE) all other occurrences.
In other computer languages this would be a CASE statement. Personally I prefer a CASE statement over ELSEIF as I find CASE easier to follow. But ELSEIF does add a degree of capability to ACTION!
In reality you can do this in most BASICs too by chaining IF...THEN...ELSE...IF... but it usually is a bit hard to follow when jammed on a single line.
 
A silly little side note but FI bugs me. I don’t know why it does but it does. OSS BASIC XL has ENDIF which is so much more, satisfying.
ENDIF; the end of IF, no more IF, stop here with the IF.
FI, what, you misspelled IF?
Fortunately you can redefine the keywords in Action! (More in another lesson), so my sense of order can be rectified. :) 
 
Next time we will look at the various types of LOOPS. (Part 2 of the program)
Till then, HLO






How about using the [ code ] tag for the source code for clearer formatting?

  • Report

I've seen the IF ... FI combination on UNIX shells, which go all the way back to the 1970s, so perhaps that's where it comes from.

 

   -dZ.

  • Report

I've seen the IF ... FI combination on UNIX shells, which go all the way back to the 1970s, so perhaps that's where it comes from.

 

   -dZ.

Hum, I didn't know that. My mainframe days were in COBOL and Burroughs servers that if they had UNIX core they hide it well.

  • Report

How about using the [ code ] tag for the source code for clearer formatting?

OK. may not get the tag on the current one I'm typing but will see about the next.

  • Report

Hum, I didn't know that. My mainframe days were in COBOL and Burroughs servers that if they had UNIX core they hide it well.

 

Yeah, it seems rather silly.  They also have "case ... esac," go figure.

  • Report