Multiple comparisons after an if statement

Post Reply
Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Multiple comparisons after an if statement

Post 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

matt7
Posts: 115
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone
Location: USA

Re: Multiple comparisons after an if statement

Post 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

Ken
Posts: 29
Joined: Tue Jan 29, 2019 1:49 am
My devices: Ipad
Location: NSW, Australia

Re: Multiple comparisons after an if statement

Post 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

User avatar
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: United States of America
Contact:

Re: Multiple comparisons after an if statement

Post 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
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)

Post Reply