Page 1 of 1

[Solved] 'Restart' keyword

Posted: Mon Dec 01, 2014 7:46 pm
by es_91
Hello.

I just programmed this...

Code: Select all

Repeat
  
  Found = #False
  
  ForEach Names$ ()
    If Names$ () = Name$
      Name$ + "_"
      Found = #True
      Break
    EndIf
  Next
  
Until Not Found
...but it would be so much quicker like this...

Code: Select all

ForEach Names$ ()
  If Names$ () = Name$
    Name$ + "_"
    Restart
  EndIf
Next
What do you think?

-------------------

Here's the above code in a testable example:

Code: Select all

NewList Names$ ()

AddElement (Names$ ())
Names$ () = "Darin"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

Name$ = "Claude"

Repeat
  
  Found = #False
  
  ForEach Names$ ()
    If Names$ () = Name$
      Name$ + "_"
      Found = #True
      Break
    EndIf
  Next
  
Until Not Found

AddElement (Names$ ())
Names$ () = Name$

ForEach Names$ ()
  
  Debug Names$ ()
  
Next

Re: 'Restart' keyword

Posted: Mon Dec 01, 2014 8:43 pm
by IdeasVacuum
In your example, there is only one name to match and so once that name is found there is no need to restart the loop. If instead your code was comparing two lists, you would use 2x ForEach and there would still be no need to restart. So, do you have an example where Restart would be invaluable?

Re: 'Restart' keyword

Posted: Mon Dec 01, 2014 8:46 pm
by es_91
I made a mistake on that example.

I edited the above code to show the effect wanted.

Re: 'Restart' keyword

Posted: Mon Dec 01, 2014 10:44 pm
by es_91
Ah, I made so much errorious stuff up there, and, as IdeasVacuum said, I was wrong ... the desired job can be done with Continue:

Code: Select all

NewList Names$ ()

AddElement (Names$ ())
Names$ () = "Darin"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

AddElement (Names$ ())
Names$ () = "Claude"

NewList NewNames$ ()

ForEach Names$ ()
  
  ForEach NewNames$ ()
    
    If NewNames$ () = Names$ ()
      
      Names$ () + "_"
      
      Continue
      
    EndIf
    
  Next
  
  AddElement (NewNames$ ())
  NewNames$ () = Names$ ()
  
Next

ForEach NewNames$ ()
 
  Debug NewNames$ ()
 
Next
I can now, after 30 minutes of stressing and confusing tests with an imaginary power that seemingly does not exists, not even say if there is a use for a Restart keyword.

Sorry for this thread.

Re: 'Restart' keyword

Posted: Mon Dec 01, 2014 11:11 pm
by Andre
Ok, so I will mark this thread as 'Solved' :D