dmsc / fastbasic

FastBasic - Fast BASIC interpreter for the Atari 8-bit computers
GNU General Public License v2.0
139 stars 20 forks source link

IF..THEN..ETC... #97

Closed rickcollette closed 1 month ago

rickcollette commented 1 month ago

I am going loopy trying to figure out the most basic of basic things in ... basic...

I would LOVE an example of this working:

A = 1
B = 0
C = 3
D = 5
E = 5

IF A = 1 THEN
  PRINT "Hello World - A is 1"
  IF B = 2 THEN
    PRINT "Hello World - B is 2"
  ELSE
    PRINT "Hello World - B is not 2"
  ENDIF
ELIF A = 2
  PRINT "Hello World - A is 2"
ELIF A = 3
  PRINT "Hello World - A is 3"
ELSE
  PRINT "Hello World - A is not 1, 2, or 3"
ENDIF

IF C = 3 AND A = 1 THEN
  PRINT "Hello World - C is 3 and A is 1"
ELIF C = 3 AND NOT B = 1
  PRINT "Hello World - C is 3 and B is not 1"
ELSE
  PRINT "Hello World - C condition not met"
ENDIF

IF D = 4 OR D = 5 THEN
  PRINT "Hello World - D is 4 or 5"
ELIF D = 6
  PRINT "Hello World - D is 6"
ELSE
  PRINT "Hello World - D is not 4, 5, or 6"
ENDIF

IF E = 5 THEN
  PRINT "Hello World - E is 5"
ELSE
  PRINT "Hello World - E is not 5"
ENDIF

END
EricCarrGH commented 1 month ago

With FastBasic, you only include THEN for single statement afterward:

IF A=1 THEN PRINT "HELLO"

If you include a : after your THEN, it will not be considered as part of the check, and will always execute. This is the opposite of Atari BASIC.

IF A=1 THEN PRINT "A IS 1": PRINT "THIS WILL DISPLAY EVEN WHEN A IS NOT 1"

If you want to span multiple lines/commands, simply remove THEN

A=1:B=2

IF A = 1
  PRINT "Hello World - A is 1"
  IF B = 2
    PRINT "Hello World - B is 2"
  ELSE
    PRINT "Hello World - B is not 2"
  ENDIF
ELIF A = 2
  PRINT "Hello World - A is 2"
ELIF A = 3
  PRINT "Hello World - A is 3"
ELSE
  PRINT "Hello World - A is not 1, 2, or 3"
ENDIF

In FastBASIC, a new line and : are considered the same thing, so the above could be written as:

IF A = 1:PRINT "Hello World - A is 1":IF B = 2:PRINT "Hello World - B is 2":ELSE:PRINT "Hello World - B is not 2"
... (I did not copy the entire thing but you get the point)

This is explained in the manual: https://github.com/dmsc/fastbasic/blob/master/manual.md#control-statements

Scroll down a bit to Conditional Execution

It took me a bit to get used to it, but now I really like this convention.

rickcollette commented 1 month ago

Excellent - thank you! Yeah - I think its a case of "slow down and get it right.." for me :)