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
.
How-to emulate "SELECT/CASE" in Control Basic...???
How-to emulate "SELECT/CASE" in Control Basic...???
Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom.
Clifford Stoll
Clifford Stoll
-
- Posts: 37
- Joined: Thu Nov 01, 2012 10:57 am
- Location: Johannesburg, South Africa
Re: How-to emulate "SELECT/CASE" in Control Basic...???
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)
For comparison, here is a IF THEN...ELSE...ENDIF version in Control Basic
of the same method:
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
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
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
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...???
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
.
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
Clifford Stoll
Re: How-to emulate "SELECT/CASE" in Control Basic...???
Hi Robert
If you don't have an OTHERWISE clause then the IF does not have to be nested.
This is a little easier to read.
Daniel
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
Daniel