Qbasicnews.com

Full Version: how does SELECT CASE work, INTERNALLY?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How would I translate SELECT CASE to IF...GOTO statements? Binary tree? :? :?
It compiles exactly the same way as equivalent IF THEN/ELSEIF THEN/ELSE/ENDIF statements. (as far as I know)

Use the /A command line switch with BC.EXE and it will put assembler in the listing file.
Yup! SELECT...CASE...END SELECT and IF...ELSEIF...ELSE...END IF structures compile exactly. But if I may add, SELECT CASE is a more cleaner way of going about things 8)
Does it compile the same way in C? If not, how does it work in C?
As far as I know, C doesn't allow variables in the case statements. So I would expect C to load the variable being examined into a register, then for each case generate a cmp/j** pair to skip over the code within if the condition isn't met.
Come to think of it, I'm starting to think that SELECT CASE works like this:

Code:
IF true THEN GOTO true statement

The reason I see it this way is because of the break; statement in C. It jumps to the correct answer, but if you don't mention break;, it'll keep going.
Quote:Does it compile the same way in C? If not, how does it work in C?

aga, i think it would be better to ask this question in the general forum or a C/C++ forum. The chances are u will get a more specific or better answer there.
in c, switch() is implemented as a jump table. i'm not sure what it is, but by what i gathered:

a. cases can be only constants
b. (expression) is evaluated ONLY ONCE, unlike in several IFs.
c. it is faster than IFs... besides, but i never realized how (except the only one-time eval)





[Flexibal>
So no chance of an on-teh-fly B tree then.....
Why would you ever use a B tree for program flow control?

Quote:[in a C switch statement] (expression) is evaluated ONLY ONCE, unlike in several IFs.
Oh right, I forgot to mention that. QB's SELECT command works the same way, it evaluates only once like C, but it doesn't use a jump table.
Pages: 1 2