IF...ELSE equivalent.

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

IF...ELSE equivalent.

Post by Shaun_B »

Hi,

A quick one: BASIC 2 doesn't have an else clause for IF statements, meaning that anything after the then command from the related if condition will be executed when the condition is true.

Although I can't think of many uses for it, there is a way to use ON ... GOTO like this:

Code: Select all

10 get a$:on -(a$="") goto 10:print "else logic here"
Commodore BASIC returns -1 if true and zero if false. So --1 turning the number positive, like the ABS function does.

You can also check for multiple conditions, like this:

Code: Select all

10 print"1) menu item 1"
20 print"2) menu item 2"
30 print"3) menu item 3"
40 get a$:on -(a$<"1" or a$>"3") goto 40:on val(a$) gosub 100, 200, 300
50 end
100 print "you chose menu item 1":return
200 print "you chose menu item 2":return
300 print "you chose menu item 3":return
The 'ELSE' clause in this case is working out which key your pressed and branching to the relevant sub-routine.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
dudz
Member
Member
Posts: 140
Joined: Tue Jun 17, 2014 5:40 am
Contact:

Re: IF...ELSE equivalent.

Post by dudz »

isnt that more of a switch/case statement than if/else?
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: IF...ELSE equivalent.

Post by ajordison »

If you're a fan of GOTOs:

Code: Select all

10 rem if then else demo
20 input a
30 if a = 1 then goto 60
40 print "this is the else part"
50 goto 70
60 print "this is the if (true) part"
70 end
Yes it's odd that the 'else' code is before the 'if true' part, but you can turn it around by negating the test:

Code: Select all

10 rem if then else demo
20 input a
30 if not (a = 1) then goto 60
40 print "this is the if (true) part"
50 goto 70
60 print "this is the else part"
70 end
Just be careful to check those GOTOs if you expand the IF and ELSE code!
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: IF...ELSE equivalent.

Post by Shaun_B »

Arthur's example is very good if you want human-readability also separate your logic over several lines. I was thinking about getting as much done in one line as possible which should be more efficient.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 2 guests