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

User avatar
RBSe
Posts: 33
Joined: Wed Mar 06, 2013 7:42 pm
Location: Eureka, California
Contact:

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

Unread post 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
.
Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom.

Clifford Stoll
Andries Pretorius
Posts: 37
Joined: Thu Nov 01, 2012 10:57 am
Location: Johannesburg, South Africa

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

Unread post 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
User avatar
RBSe
Posts: 33
Joined: Wed Mar 06, 2013 7:42 pm
Location: Eureka, California
Contact:

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

Unread post 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
.
Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom.

Clifford Stoll
Daniel
Posts: 133
Joined: Tue Nov 13, 2012 2:10 pm

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

Unread post 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
Post Reply