Is there a command in Smart Basic to replace multiple comparisons after an if statement.
Say
if z<100 and z>41 and y>32 and y<350 and ready = 1 then goto continue:
They look cluttered and hard to read.
I think it used to be Case
Ken
Multiple comparisons after an if statement
Re: Multiple comparisons after an if statement
I don't know of any command that can compress the notation you are using (a series of ANDs).
Some options to think about for improving readability:
Unfortunately, there is no SWITCH...CASE statement in smart Basic. It is a little messy and annoying to keep track of how many END IFs I need at the end, but I use the following style for switching on a variable value (this is my personal way to implement an IF...ELSE IF...ELSE structure that avoids accumulating indents with each ELSE IF):
Some options to think about for improving readability:
- Separate some of the conditions into different IFs (that are nested). For example, the "ready" condition seems qualitatively different than the range conditions, so it might be logical to separate it from the other conditions. It probably makes sense to check the ready variable first, but that depends on the specific application.
- Create a function that combines related conditions (ones with shared variables). For example, you could create an IN_RANGE(val, lo, hi) function for checking if a value falls between a lower and upper bound.
Code: Select all
DEF IN_RANGE (val, lo, hi)
IF (val > lo) AND (val < hi) THEN
RETURN 1
ELSE
RETURN 0
END IF
END DEF
' Option 1
IF ready THEN
IF IN_RANGE(z,41,100) AND IN_RANGE(y,32,350) THEN
' . . .
END IF
END IF
' Option 2
zIn = IN_RANGE(z,41,100)
yIn = IN_RANGE(y,32,350)
IF zIn AND yIn AND ready THEN
' . . .
END IF
' Option 3
IF ready THEN
zIn = IN_RANGE(z,41,100)
yIn = IN_RANGE(y,32,350)
IF zIn AND yIn THEN
' . . .
END IF
END IF
Code: Select all
IF a = 1 THEN
' do something
ELSE ! IF a = 2 THEN
' do something
ELSE ! IF a = 3 THEN
' do something
ELSE
' default case
END IF ! END IF ! END IF
Re: Multiple comparisons after an if statement
Hi Thanks
I will persevere with the multiple comparisons rather than the nested if ..... thens.
I am sure I remember from Visual Basic Apple Basic or Commodore Basic etc a case command
Ken
I will persevere with the multiple comparisons rather than the nested if ..... thens.
I am sure I remember from Visual Basic Apple Basic or Commodore Basic etc a case command
Ken
- GeorgeMcGinn
- Posts: 435
- Joined: Sat Sep 10, 2016 6:37 am
- My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10 - Location: Venice, FL
- Flag:
- Contact:
Re: Multiple comparisons after an if statement
I noticed that your first program has multiple IF/THEN/ELSE/ENDIF.
If you want to create a case statement check out program #1 below.
Here I am testing a=1 THEN I do one item. If your THEN is followed by one statement, no ENDIF is needed. You can, as in my examples, stack as many IF statements without having to use an ENDIF.
However if you have multiple statements to execute after that THEN, then you can actually replace the a=a+1 with a GOSUB and then execute all the statements like you would after a case statement where you test a single condition.
This works if you have a statement that tests multiple conditions (see program #2)
This way, if you replace "a=a+1" with a GOSUB, then you execute multiple lines of code, and not have to use any ENDIF's. The code looks cleaner as you IF statements, like a CASE statement,usually checks one or two variables for different values.
Program #1
Program #2
If you want to create a case statement check out program #1 below.
Here I am testing a=1 THEN I do one item. If your THEN is followed by one statement, no ENDIF is needed. You can, as in my examples, stack as many IF statements without having to use an ENDIF.
However if you have multiple statements to execute after that THEN, then you can actually replace the a=a+1 with a GOSUB and then execute all the statements like you would after a case statement where you test a single condition.
This works if you have a statement that tests multiple conditions (see program #2)
This way, if you replace "a=a+1" with a GOSUB, then you execute multiple lines of code, and not have to use any ENDIF's. The code looks cleaner as you IF statements, like a CASE statement,usually checks one or two variables for different values.
Program #1
Code: Select all
IF a = 1 THEN a=a+1
IF a = 2 THEN a=a+1
IF a = 3 THEN a=a+1
IF a > 3 THEN a=1
PRINT a
END
Code: Select all
a=1!b=1
IF a=1 AND b=1 THEN a=a+1
IF a=2 AND b=1 THEN a=a+1
IF a=3 AND b=1 THEN a=a+1
IF a>3 OR a=0 THEN a=1+b
PRINT a
END
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)