Emulating a LABEL or GOTO (Solved)

Nakita Block
01 Sept 2018, 09:13

I certainly apologize for all my questions. Honestly I do read as much as I can and try to find answers for myself. Many questions I do find the answers for. I'm not seeing this one.

I'm just trying to find or create an equivalent to a Label or a way to GOTO something. I'll try to give an example.

I will shuffle a combination. But I don't want any of the numbers to be the same. We'll just say 6 numbers, all random from 0 to 5.

Create LIST
Add first Random Number (0-5) to LIST
 
Create FOR/NEXT from 1 to 5 for the next numbers
r = Random Number
Nest another FOR/NEXT to check previous numbers to see if r = any of those numbers
If it does, go back and generate another number.

So how can I make it go back and create another number?


mrangel
01 Sept 2018, 09:44

If you want it to go back, that's what a while loop is good for:

numbers = NewList()
list add (numbers, GetRandomInt(0, 5))

for (i, 1, 5) {
  isduplicate = true
  while (isduplicate) {
    r = GetRandomInt (0, 5)
    isduplicate = false
    foreach (existing_number, numbers) {
      if (existing_number = r) {
        isduplicate = true
      }
    }
  }
  list add (numbers, r)
}

However, in this case you don't need the inner loop, and you can make it a lot smoother:

numbers = NewList()
r = GetRandomInt (0, 5)
list add (numbers, r)

for (i, 1, 5) {
  while (ListContains (numbers, r)) {
    r = GetRandomInt (0,5)
  }
  list add (numbers, r)
}

Or, a more common way to perturb a list:

remaining_numbers = Split("0;1;2;3;4;5")
shuffled_numbers = NewList()
for (1, 0, 5) {
  r = PickOneString (remaining_numbers)
  list remove (remaining_numbers, r)
  list add (shuffled_numbers, ToInt(r))
}

Sorry to say, there isn't a GOTO statement. That's pretty normal for any modern programming language.
But on the bright side, asking "How can I do this without GOTO" nearly always results in a more efficient algorithm.

A brief list of situations where you might want to use goto if you started programming on a language that has it, and the usual recommendations for how to deal with them:

  • If you want to jump back, you could try using a while loop, setting a boolean variable to let Quest know it should repeat
  • If you want to jump forward, just use an if statement to skip over the code in between
  • If you want to jump out of a loop, that's actually one place where Quest falls down. Most languages have statements with names like "break", "last", "redo", or "continue" which allow you to exit the loop you're currently in. Quest doesn't seem to do that. However, if you put the loop in a function, you can use returnto get out of it immediately.
  • If you want to jump into a loop, why would you want to do that? What would the loop variables be set to? In languages that allow this, doing it is usually taken as a sign that you've done something dumb. The only counterexample I can think of is Duff's device in C, which puts a while loop in between a switch statement and its cases.
  • If you're doing something more complicated, try rethinking your code. If your plan requires a jump that's too complex to use one of the methods above, there is probably a better plan. There may be exceptions, in which case you can combine the other methods, but you should sit down and look at your plan first, and try to work out if there is a simpler way to accomplish your task.

Nakita Block
01 Sept 2018, 13:39

I can't believe I didn't think of While. Slap me with the idiot stick. Thank you once again.

In my defense I have really only mingled with BASIC most of my life.