# How build a loop like this (compound loop)

## Recommended Posts

I'm trying to solve a function but not'm getting.
Someone can help-me?
This simplified form:

You run the function `foo`
The function foo contains two functions: function1 () and function2 ()

The results of the functions function1 () and function2 () are:
If it worked, it returns something data.
If you went wrong, set some value in @error.

Now the details:

The function function1 () loop in between, and will not be executed until having @error
The function function2 () and executed, and give @error, it executes function1 () function again.
The function function2 () will be executed until not @error present.
Then the function `foo` function returns the result of function2 ().

Maybe this draw can help.

```foo()

Func foo()
Local \$try = 0
Local \$aa = 0, \$bb = 0
ConsoleWrite("start" & @LF)
Do
Switch \$try
Case 0, 2
\$aa = function1()
If @error Then
\$try = 1
Else
\$try = 0
\$bb = function2()
If @error Then
\$try = 2
Else
\$try = 0
EndIf
EndIf
ConsoleWrite("\$TRY[" & \$try & "]" & @LF)
Case 1
\$aa = function1()
If @error Then
\$try = 1
Else
\$try = 0
EndIf
ConsoleWrite("\$TRY[" & \$try & "]" & @LF)
EndSwitch

Until Not \$try

EndFunc   ;==>wer

Func function1()
If Random(0, 1, 1) Then Return 1
Return SetError(1, 0, 0)
EndFunc   ;==>aa

Func function2()
If Random(0, 1, 1) Then Return 1
Return SetError(1, 0, 0)
EndFunc   ;==>bb```
Edited by Detefon

Visit my repository

##### Share on other sites

```While 1
Do
Function1()
Until Not @error
Function2()
If Not @error Then ExitLoop
WEnd```

##### Share on other sites

bahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...

@guinness, this is not funny... thank you!

Visit my repository

##### Share on other sites

Yes, very complicated... I lost one hour to solve this problem, your give a great help! ^^

```Local \$result = foo()
; \$result must be = 2
ConsoleWrite("\$result[" & \$result & "]" & @LF)

Func foo()
Local \$function_1 = 0, \$function_2 = 0
While 1
Do
ConsoleWrite(1 & @LF)
\$function_1 = function1()
Until Not @error And Sleep(1000)
ConsoleWrite(2 & @LF)
\$function_2 = function2()
If Not @error Then ExitLoop
Sleep(1000)
WEnd
Return \$function_2
EndFunc   ;==>foo

Func function1()
If Random(0, 1, 1) Then Return 1
Return SetError(1, 0, 0)
EndFunc   ;==>function1

Func function2()
If Random(0, 1, 1) Then Return 2
Return SetError(1, 0, 0)
EndFunc   ;==>function2```
Edited by Detefon

Visit my repository

##### Share on other sites

Yes, very complicated... I lost one hour to solve this problem, your give a great help! ^^

Not everyone would agree with you.

PS Maybe Sleep(100) is suffice.

##### Share on other sites
```While 1
Do
Function1()
Until Not @error
Function2()
If Not @error Then ExitLoop
WEnd```

or like this?

```Do
Do
Function1()
Until Not @error
Function2()
Until @error```

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

like this should work...... (maybe)

```Do
Do
Function1()
Until Not @error
Function2()
Until Not @error```

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

PS Maybe Sleep(100) is suffice.

I'll remember that, but at this point of development I still do not have a better idea of the value.

Visit my repository

##### Share on other sites

like this should work...... (maybe)

```Do
Do
Function1()
Until Not @error
Function2()
Until Not @error```

Perhaps. I only went with a while and do because I like to go crazy!

##### Share on other sites

Thank you: @guinness & @Chimp

Visit my repository

## Create an account

Register a new account

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...