Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how does SELECT CASE work, INTERNALLY?
#1
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.
Reply
#2
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.
Reply
#3
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)
Reply
#4
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.
Reply
#5
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.
Reply
#6
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.
Reply
#7
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.
Reply
#8
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>
Reply
#9
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.
Reply
#10
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)