Page 1 of 1

How-to emulate "SELECT/CASE" in Control Basic...???

PostPosted: Tue Jun 18, 2013 5:38 pm
by RBSe
Greetings,

I noticed there is no SELECT/CASE command in control basic.
How-to emulate "SELECT/CASE" in Control Basic...???
I'm not an experienced programmer, that's why I ask...
Thanks for your help.

Robert
.

Re: How-to emulate "SELECT/CASE" in Control Basic...???

PostPosted: Tue Jun 18, 2013 9:55 pm
by Andries Pretorius
Hi Robert

The following examples test a numeric variable and displays a message
with a word in it:

A hypothetical SELECT CASE... ENDCASE structure may look like this:
(There are variations between different languages and dialects)
Code: Select all
SELECT CASE
    CASE vResult =1    ` Test if the number is 1
        PRINT "One."  ` If it is 1, display a message
    CASE vResult = 2   ` Test if the number is 2
        PRINT  "Two."  ` If it is 2, display a message
    CASE vResult = 3 ` Test if the number is 3
        PRINT "Three."   ` If it is 3, display a message
    OTHERWISE   ` If it is not 1, 2, or 3, display a message
        PRINT "It was not one, two, or three."
ENDCASE


For comparison, here is a IF THEN...ELSE...ENDIF version in Control Basic
of the same method:

Code: Select all
IF vResult = 1  THEN    ' Testst if the number is 1
    PRINT "One."       'If it is 1, display a message
ELSE
    IF vResult = 2 THEN  ' Test if the number is 2
        PRINT "Two."       ' If it is 2, display a message
    ELSE
        IF vResult = 3   THEN    ' Test if the number is 3
            PRINT "Three."       ' display a message
        ELSE   ' If it is not 1, 2, or 3, display a message
            PRINT "It was not one, two, or three."
        ENDIF
    ENDIF
ENDIF


Downsides of the IF THEN...ELSE...ENDIF structure are that it is not as intuitive and it does not look
as elegant as the SELECT CASE... ENDCASE structure.

Remember that with a SELECT CASE...OTHERWISE...ENDCASE method, only the first TRUE
case is executed. Even if two or more cases are TRUE, only the statements
following the first TRUE case will be executed.

In the IF THEN...ELSE...ENDIF method, each case is evaluated in succession.
One or more cases can be TRUE

Hope this helps

Best regards

Andries

Re: How-to emulate "SELECT/CASE" in Control Basic...???

PostPosted: Thu Jun 20, 2013 5:28 pm
by RBSe
Thanks Andries,

That is a very good explanation.
I would think the vectoring aspect of select/case would save run time not having to test every condition.
But am I talking about a very small amount of time here?

Robert
.

Re: How-to emulate "SELECT/CASE" in Control Basic...???

PostPosted: Thu Jun 20, 2013 8:16 pm
by Daniel
Hi Robert

If you don't have an OTHERWISE clause then the IF does not have to be nested.

Code: Select all
IF vResult =1 THEN
   PRINT "One."  ` If it is 1, display a message
ENDIF
IF vResult =2 THEN
   PRINT "Two."  ` If it is 2, display a message
ENDIF
'Emulate OTHERWISE
IF (vResult =1) OR (vResult =2) THEN
   'Emty THEN
ELSE
   PRINT "It was not one or two"
ENDIF


This is a little easier to read.

Daniel