Jump to content
Sign in to follow this  
RSS Bot

HLO projects - Learning Action! Part 3; If Then

Recommended Posts

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. V is a byte type. The () at the end are not optional.

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. All you have to do is end it with the command FI (which is IF backwards).
You don't have to have an ELSE, it is optional. But you do need a FI even if its just one line. You have to close off the IF statement.
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 ....., 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 when mixing types.
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

With ELSEIF what this enables you to do is specify several conditions for condition. 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



http://atariage.com/forums/blog/528/entry-15436-learning-action-part-3;-if-then/

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...