End a for loop early in ebasic?

588 views
Skip to first unread message

Adam Osth

unread,
Sep 11, 2009, 2:13:03 PM9/11/09
to e-p...@googlegroups.com
Hello all,

I have a program in e-basic that takes two arrays and combines them, then randomizes and recombines them. Since I can't reuse any combinations that were in place previously, I have a loop structure in place to detect whether the combinations have been used. It goes something like this.

HeadColor(7) As String
HeadShape(7) As String
(takes values for each)
For a = 0 to a = 7
 Heads(a) = HeadColor(a) + HeadShape(a)

/*randomize and check for repeats
Nomatch As Boolean
Nomatch = false
Matches As Integer

While Nomatch = false
 Randomize HeadColor
 Randomize HeadShape
 For b = 0 to b = 7
  If HeadColor(b) + HeadShape(b) = Heads(b)
   Matches = Matches + 1

  End If
  Next b
 If Matches = 0
  Nomatch = true
Wend

Now -- in the highlighted FOR loop, if the If statement is true, it is basically saying that the entire While loop needs to be repeated and re-randomized (a match is present). Thus, the rest of the For loop is irrelevant. Is there a statement that can be used to end the for loop, like a break or end statement?

Also, if my code is horribly inefficient, feel free to critique me as I'm not a trained programmer.

Any help would be appreciated! Thanks!

Adam
  

David McFarlane

unread,
Sep 14, 2009, 5:13:19 PM9/14/09
to e-p...@googlegroups.com
Adam,

You may use Exit For to get out of a For loop early, please see the
"Exit (keyword)" topic in the E-Basic online help and take it from there.

On a style note, for some reason even young E-Prime programmers seem
to latch on to the quaint and long deprecated While...Wend statement
instead of using the much more serviceable and preferred Do...Loop
construct (please see, e.g.,
http://msdn.microsoft.com/en-us/library/661sy9w2(VS.85).aspx ). I
would ask everyone to abandon While...Wend in favor of Do...Loop;
While...Wend went out with the Commodore 64!

Finally, as in PST's NoRepeat.es example, you use a non-deterministic
algorithm instead of a deterministic algorithm. I have discussed
this elsewhere. But a deterministic algorithm, though arguably more
efficient, takes some intricate programming, and your sample size is
so small that I do not think it worth the effort. So I would have
you continue with what you are doing.

-- David McFarlane, Professional Faultfinder

Reply all
Reply to author
Forward
0 new messages