# Creating a dynamic number of loops

## Recommended Posts

Hello autoit friends,

howto create a dynamic number of loops?

It should like like (e.g. for a number of 3 loops):

;~~~~~~~~~~~~~~~~~~

For \$loop1 = 1 to 10

For \$loop2 = 1 to 10

For \$loop3 = 1 to 10

; do something

next

next

next

;~~~~~~~~~~~~~~~~~~

But how to make it DYNAMIC ?

Any ideas?

Carsten

Edited by fielmann

##### Share on other sites

is this what you want?

```\$min=Random(1,10,1)
\$max=Random(10,100,1)

For \$i=\$min to \$max
;do stuff
Next```

if not then try to explain better what you want..

Only two things are infinite, the universe and human stupidity, and i'm not sure about the former -Alber EinsteinPractice makes perfect! but nobody's perfect so why practice at all?http://forum.ambrozie.ro

##### Share on other sites

Hello autoit friends,

howto create a dynamic number of loops?

It should like like (e.g. for a number of 3 loops):

;~~~~~~~~~~~~~~~~~~

For \$loop1 = 1 to 10

For \$loop2 = 1 to 10

For \$loop3 = 1 to 10

; do something

next

next

next

;~~~~~~~~~~~~~~~~~~

But how to make it DYNAMIC ?

Any ideas?

Carsten

You need some sort of recursive function. Here is a simple example

```Dim \$a[4] = [3,4,5,2];the upper count for each loop
Dim \$r[4]
Global \$x=0
startloop(\$a,0)

Func startloop(\$b,\$c)
Local \$n
If \$c < UBound(\$b) Then
For \$n = 1 To \$b[\$c]
\$r[\$c] = \$n
Startloop(\$b,\$c + 1)
Next
Else
Dosomething()
EndIf
EndFunc
Func Dosomething()
\$x += 1
ConsoleWrite("loop vals are ")
For \$p = 1 To UBound(\$a)
ConsoleWrite(\$r[\$p - 1] & ', ')
Next
ConsoleWrite( "count = " & \$x & @CRLF)
EndFunc```

BTW, welcome to the forums; an interesting first question.

EDIT: improved example a bit

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

##### Share on other sites

You need some sort of recursive function. Here is a simple example

[...]

BTW, welcome to the forums; an interesting first question.

EDIT: improved example a bit

Hello Martin!

thx 4 reply an the example with the recursive function.

that was exactly what I'm seraching for

I work several years with autoit 2 + 3, but I never realized this forum.

## Create an account

Register a new account

×

• Wiki

• Back

• Git