End a for loop early in ebasic?

David McFarlane mcfarla9 at msu.edu
Mon Sep 14 21:13:19 UTC 2009


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


At 9/11/2009 02:13 PM Friday, you wrote:
>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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To post to this group, send email to e-prime at googlegroups.com
To unsubscribe from this group, send email to e-prime+unsubscribe at googlegroups.com
For more options, visit this group at http://groups.google.com/group/e-prime?hl=en
-~----------~----~----~----~------~----~------~--~---



More information about the Eprime mailing list