Jump to content

Can the lable used in a continue statement be a variable?


MikeatW3S
 Share

Recommended Posts

Suppose I'd like to jump out of a loop to a number of different places in the code. Each of those places start under a different label. Can I create a variable that somehow evaluates to the text of one of the label names and use that variable in the "continue label;" to jump to the code under that label? Or will continue only use the text of the label and not consider what text that label may evaluate to?

Edited by MikeatW3S
Link to comment
Share on other sites

I'd like to execute code depending on some expression, and no other code than that. I don't want to go through a lot of checking before deciding whether or not to execute each section of code. I'd like to simply evaluate and expression, and depending on what it is go directly to the appropriate code. How do I do that?

Edited by MikeatW3S
Link to comment
Share on other sites

Papers have been written about how that's a bad design, you're essentially using goto.

I don't want to go through a lot of checking before deciding whether or not to execute each section of code.

It sounds like you need to check regardless, you're just moving the location of the checks. If you want to show your code there's a way to avoid doing that.I don't think there's a way to use a variable as a label.
Link to comment
Share on other sites

Papers have been written about how that's a bad design, you're essentially using goto. It sounds like you need to check regardless, you're just moving the location of the checks. If you want to show your code there's a way to avoid doing that.I don't think there's a way to use a variable as a label.

Yes, I know goto is not best practice, messy code, hard to follow, etc.

 

But how else would one construct a vector jump table, that jumps to some address/label based on the offset into some table, i.e. based on some expression? Otherwise, I may have hundreds of decisions to make every 10th of a second. I don't know how much that may unnecessarily overwork the processor.

Edited by MikeatW3S
Link to comment
Share on other sites

I was thinking more in the lines of a hash-table like this:

 

var foo = '35'; // where 35 is the number of tenths of a second into the audio file.

 

var cases = {}; cases['10'] = function() { code for 1.0s; }; cases['25'] = function() { code for 2.5s; }; cases['35'] = function() { code for 3.5s; }if(typeof cases[foo] == 'function') { // only executes for foo = 3.5 seconds. cases[foo]();}

else { // default (the fallthrough)}

 

 

Then only the code associated with a time to change text will execute, and nothing more. Of course I'd have to execute the if(typeof cases[foo] == 'function') { code every 10th of a second, or faster.

 

This looks like what a jump table would do. But I'm not sure what the processor would do. Would a processor actually go through the list from top to the right case['35']? After all, a processor has to find cases['35'] out of all the possibilities, right?

Edited by MikeatW3S
Link to comment
Share on other sites

I think a data structure like that is actually stored similar to a hash table, so it should have random access. That could be pretty easy to test though by building very small and large data structures and seeing how quick it is to access a specific element.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...