Saturday, July 13, 2024

📚 A Summary of Conditionals in BAM

Page Contents:

  • Conditional Single-Line Structures
  • Conditional Block Structures
  • Conditional Functions
  • References

Conditional Single-Line Structures

  • IF...THEN LineIdentifier 
Example: 
25 PRINT "howdy";
SLEEP 0.5
IF TRUE THEN 25
  • IF...GOTO LineIdentifier
Example: 
25 PRINT "howdy";
SLEEP 0.5
IF TRUE GOTO 25
  • IF...THEN SingleStatement
Example: 
IF TRUE THEN PRINT "Howdy"
  • IF...THEN MultiStatement
Example: 
IF TRUE THEN PRINT "Howdy"; : PRINT " There"
  • IF...THEN...ELSE SingleStatements
Example: 
IF TRUE THEN PRINT "Yup" ELSE PRINT "Nope"
  • ON GOTO
Example:
1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) ) + 1
2 ON v% GOTO 10,11,12
3 GOTO 1
10 COLOR 15 : PRINT "0 "; : GOTO 20
11 COLOR 14 : PRINT "1 "; : GOTO 20
12 COLOR 13 : PRINT "2 "; : GOTO 20
20 SLEEP 0.001 : GOTO 1
  • ON GOSUB
  • ON RESTORE
  • GOTO "EVAL" Clause
Example:
FUNCTION WhereToGo%(v%)
  SELECT CASE v%
    CASE 2    : WhereToGo% = 140
    CASE 5    : WhereToGo% = 190
    CASE 9    : WhereToGo% = 250
    CASE ELSE : WhereToGo% = 300
  END SELECT
END FUNCTION
  1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) )
    GOTO EVAL( WhereToGo%(v%) )
140 COLOR 15 : PRINT "2 "; : GOTO 300
190 COLOR 14 : PRINT "5 "; : GOTO 300
250 COLOR 13 : PRINT "9 "; : GOTO 300
300 SLEEP 0.001 : GOTO 1
  • GOSUB "EVAL" Clause
  • RESTORE "EVAL" Clause

Conditional Block Structure

  • IF...THEN Block
Example:
1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) )
  IF     v% = 0 THEN : COLOR 15 : PRINT "0 ";
  ELSEIF v% = 1 THEN : COLOR 14 : PRINT "1 ";
  ELSEIF v% = 2 THEN : COLOR 13 : PRINT "2 ";
  ELSE               : COLOR 12 : PRINT "? ";
  END IF
  SLEEP 0.21
  GOTO 1
  • SELECT CASE Block
Example: 
1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) )
  SELECT CASE v%
    CASE    0 : COLOR 15 : PRINT "0 ";
    CASE    1 : COLOR 14 : PRINT "1 ";
    CASE    2 : COLOR 13 : PRINT "2 ";
    CASE ELSE : COLOR 12 : PRINT "? ";
  END SELECT
  SLEEP 0.21
  GOTO 1

  • WHILE...WEND

Example: 
WHILE i% < 25
  i% = i% + 1
  PRINT i%
  SLEEP 0.1
WEND
END

Alternative Keyword Delimiters: 

    • DO WHILE...WEND
    • WHILE...LOOP
    • DO WHILE...LOOP
  • DO...LOOP

Example: 
DO
  i% = i% + 1
  PRINT i%
  SLEEP 0.1
  IF i% = 25 THEN END
LOOP

  • DO...LOOP WHILE

Example: 
DO
  i% = i% + 1
  PRINT i%
  SLEEP 0.1
LOOP WHILE i% < 25
END

  • DO...LOOP UNTIL

Example: 
DO
  i% = i% + 1
  PRINT i%
  SLEEP 0.1
LOOP UNTIL i% = 25
END

Conditional Functions

  • IFF
Example:
PRINT IFF( TRUE, "Yup", "Nope")
  • CHOOSE
Example:
1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) ) + 1
  PRINT CHOOSE( v%, "Zero ", "One  ", "Two  " );
  SLEEP 0.21 : GOTO 1
  • NVL$
Example:
1 v% = VAL( RIGHT$( STR$( INT( TIMER * 10 ) ), 1) ) + 1
  PRINT NVL$( CHOOSE( v%,"Zero ","One  ","Two  " ), "???  " );
  SLEEP 0.21 : GOTO 1
  • MAX
Example:
a% = 5 : b% = 10
PRINT MAX( a%, b% )
  • MIN
Example:
a% = 5 : b% = 10
PRINT MIN( a%, b% )

 

References:

No comments:

Post a Comment

🖥 Fan Dance

This is a port and mod of a SpecBAS program by ZXDunny. Run the program View the source code And, a "less is more?" version: Run t...