Posts: 3,368
Threads: 195
Joined: Jan 2003
How would I translate SELECT CASE to IF...GOTO statements? Binary tree? :? :?
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,272
Threads: 36
Joined: Feb 2003
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.
Posts: 4,307
Threads: 63
Joined: May 2003
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)
Posts: 3,368
Threads: 195
Joined: Jan 2003
Does it compile the same way in C? If not, how does it work in C?
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,272
Threads: 36
Joined: Feb 2003
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.
Posts: 1,813
Threads: 284
Joined: Mar 2003
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.
earn.
Posts: 4,307
Threads: 63
Joined: May 2003
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.
Posts: 122
Threads: 21
Joined: Dec 2002
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>
Posts: 3,368
Threads: 195
Joined: Jan 2003
So no chance of an on-teh-fly B tree then.....
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 1,272
Threads: 36
Joined: Feb 2003
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.