Page 1 of 1
Multiple comparisons after an if statement
Posted: Wed Feb 06, 2019 10:41 pm
by Ken
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
Re: Multiple comparisons after an if statement
Posted: Thu Feb 07, 2019 1:48 am
by matt7
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:
- 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.
For your example, your single line of code isn't too bad (just 5 simple conditions), but I realize that may just be a simplified example for the purpose of asking your question. So here are the above suggestions applied to your example. It makes this particular example look overly complicated, but for other situations, these strategies may help.
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
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):
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
Posted: Thu Feb 07, 2019 2:16 am
by Ken
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
Re: Multiple comparisons after an if statement
Posted: Thu Feb 07, 2019 10:12 am
by GeorgeMcGinn
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
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
Program #2
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
Ken wrote: ↑Thu Feb 07, 2019 2:16 am
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