Jump to content

How To Continue A 'for' Loop From A Function


Recommended Posts

Beware! This is going to be a stupid one. :whistle:

For $Iteration=1 to 5
proc1 ()
proc2 ()
proc3 ()
proc4 ()
Next
exit

func proc1 ()
msgbox (0,"","1")
endfunc

func proc2 ()
;some code here
;WHAT TO PUT HERE IN ORDER TO CONTINUE TO THE NEXT 'FOR' ITERATION FROM THIS FUNCTION??
endfunc

func proc3 ()
msgbox (0,"","3")
endfunc

func proc4 ()
msgbox (0,"","4")
endfunc
Link to comment
Share on other sites

  • Developers

For $Iteration=1 to 5
   proc1 ()
   if proc2 () = 1 then
      proc3 ()
      proc4 ()
   endif
Next
exit

func proc1 ()
   msgbox (0,"","1")
endfunc

func proc2 ()
  ;some code here
  ;WHAT TO PUT HERE IN ORDER TO CONTINUE TO THE NEXT 'FOR' ITERATION FROM THIS FUNCTION??
   if "...this is true then loop..."  then
      return(0)
   else
      return(1)
   endif
endfunc

func proc3 ()
   msgbox (0,"","3")
endfunc

func proc4 ()
   msgbox (0,"","4")
endfunc

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

is this what you would like to do ??

For $Iteration=1 to 5
   $t = 0
   proc1 ()
   if $t = 0 then proc2 ()
   if $t = 0 then proc3 ()
   if $t = 0 then proc4 ()
Next
exit

func proc1 ()
   if "...this is true then loop..."  then $t=1
   msgbox (0,"","1")
endfunc

func proc2 ()
  ;some code here
  ;WHAT TO PUT HERE IN ORDER TO CONTINUE TO THE NEXT 'FOR' ITERATION FROM THIS FUNCTION??
   if "...this is true then loop..."  then $t=1
endfunc

func proc3 ()
   if "...this is true then loop..."  then $t=1
   msgbox (0,"","3")
endfunc

func proc4 ()
   msgbox (0,"","4")
endfunc
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Yep. This is it. The issue is that in AutoIt 91 (and greather), it is no more (but justified) possible to use NEXT from a function in order to continue a for Loop, so I thought there is a new command that can do it (in order to avoid all the "IF".

10x JdeB. I'll use your suggestion.

Link to comment
Share on other sites

  • Developers

jon added more syntax checking in 3.0.88 .. one of them was:

- Changed: All block structures (IF/WHILE/FOR, etc.) are now checked before execution.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

JdeB, in your second example, does $t need to be declared as a Global variable?

Here's another (but related) idea that depends on the return values of each proc:

For $Iteration=1 to 5
  proc1 ()
  If proc2 () then ContinueLoop
  If proc3 () then ContinueLoop
  If proc4 () then ContinueLoop
Next

Edit: fixed code /code tags

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

  • Developers

CyberClug,

When a Variable is used before calling the Function it is know at the function level so guess can be considered Global.

try the below script...

Forgot about the ContinueLoop :whistle: ... agree that its better to use it as you show and use the Return(1) in stead of $t=1 in the function.

For $Iteration=1 to 4
  $t = 0
  proc1 ()
  if $t = 0 then proc2 ()
  if $t = 0 then proc3 ()
  if $t = 0 then proc4 ()
Next
exit

func proc1 ()
  if $Iteration = 1  then $t=1
  msgbox (0,"","1")
endfunc

func proc2 ()
  if $Iteration = 2  then $t=1
  msgbox (0,"","2")
endfunc

func proc3 ()
  if $Iteration = 3  then $t=1
  msgbox (0,"","3")
endfunc

func proc4 ()
  msgbox (0,"","4")
endfunc
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Doesn't ContinueLoop restart the loop from the top, thus none of the code below will be executed? I can't remember, I haven't used it in awhile, but I thought it was the same as C's contine keyword which restarts the loop again.

Link to comment
Share on other sites

Doesn't ContinueLoop restart the loop from the top, thus none of the code below will be executed?  I can't remember, I haven't used it in awhile, but I thought it was the same as C's contine keyword which restarts the loop again.

From the help file:
;Print all numbers from 1 to 10 except number 7
For $i = 1 to 10
    If $i = 7 Then ContinueLoop
    MsgBox(0, "The value of $i is:", $i)
Next

I think that is what BlueScreen wants.... Try it out after you get your computer working!

Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Thanks CyberSlug, I see that I was correct on what ContinueLoop did (Yay for my memory working for once), but was incorrect on what was trying to be achieved. I just went back and re-read some of the posts and see that I interpreted the goal wrong. Now that I've taken a second look, I also think that is what he wanted to do. It threw me off slightly seeing so many "If function Then ContinueLoop" statements in a row.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...