for next problem

  • Thread starter Thread starter andreas
  • Start date Start date
A

andreas

Hi,
I want to do some calculation like
( t1 and t2 are known)

for i = t1 to t2
for j = t1 to t2
.....
.....
for p = t1 to t2
for q = t1 to t2
some actions
next
next
.......
next
next

but i know only the number for next in run time with the variable iTimes
Is there a solution for this problem?
Thanks for any response
 
andreas said:
I want to do some calculation like
( t1 and t2 are known)

for i = t1 to t2
for j = t1 to t2
.....
.....
for p = t1 to t2
for q = t1 to t2
some actions
next
next
.......
next
next

but i know only the number for next in run time with the variable iTimes
Is there a solution for this problem?

You want iTimes nested 'For...To' loops?
 
And those actions are ? How do they depend on loop variables ? IMO this is
the main problem...

For exaple you could just use a single loop (doing 4 times 10 loops would be
the same than doing a loop 40 times)
 
it is not possible to change the 4 times 10 loops in 40 loops because i do
something with i, j,k ...
 
If you do something with an unknown number of variables you could use an
array instead of using i,j,k etc... You'll just have a single loop in which
you'll increment the appropriate element of the array for use in your
actions...

If you prefer and have a maximum possible number of loops you could also use
start=stop for some of the loop so that they are done only once when
appropriate etc...
 
andreas said:
I want to do some calculation like
( t1 and t2 are known)

for i = t1 to t2
for j = t1 to t2
.....
.....
for p = t1 to t2
for q = t1 to t2
some actions
next
next
.......
next
next

but i know only the number for next in run time with the variable iTimes
<snip>

One possible approach would be to use a single function controlled by
iTimes: if iTimes was 0, it would perform the specified action,
otherwise it would call itself recursevelly, decrementing iTimes. The
matters complicate a little because you want the respective indexes of
each loop, but these can be kept in a stack and accessed by the
'action' sub.

An air-code example of this approach could be:

Class NestedLoop
Protected ReadOnly Stack As New Stack(Of Integer)
Protected ReadOnly Min As Integer
Protected ReadOnly Max As Integer
Protected Levels As Integer

Sub New(Levels As Integer, Min As Integer, Max As Integer)
Me.Min = Min
Me.Max = Max
Me.Levels = Levels
End Sub

Protected Sub Execute
DoLoop(Levels)
End Sub

Protected Overridable Sub Action()
End Sub

Private Sub DoLoop(Level As Integer)
If Level = 0 Then
Action
Else
For I As Integer = Min To Max
Stack.Push(I)
DoLoop(Level - 1)
I = Stack.Pop
Next
End If
End Sub
End Class

Then, it would be just a matter of inheriting from the class and
overriding the method Action, adding to the derived class whatever new
properties you'd like.

Class SimpleLoop
Inherits NestedLoop
Public ReadOnly Result As Integer

Public Sub New(Levels As Integer, Min As Integer, Max As Integer)
MyBase.New(Levels, Min, Max)
Execute
End Sub

Protected Overrides Sub Action
Result += 1
End Sub
End Class

Finally, to invoke the action, you could use something like this:

Dim ActionResult As New SimpleLoop(iTimes, T1, T2)

Presto! =)

HTH.

Regards,

Branco.
 
Branco,
Not easy for me to understand all but I think this is the solution for my
problem.
Thank you and everyone.
 
andreas said:
Branco,
Not easy for me to understand all but I think this is the solution for my
problem.

It's actually quite simple.

When you create an instance of a class derived from NestedLoop, it
initializes the loop parameters with a call to MyBase.New(). The class
must then call the protected method Execute, which will actually start
the loop by calling the private method DoLoop in the base class.

The DoLopp method just checks the current nest level: if it's 0 then it
calls the virtual method Action, otherwise it starts a new loop in the
specified range and calls itself recursivelly, decrementing Level as it
goes. This way there will be N recursive DoLoop calls, corresponding to
the number of levels you specified. For all effects, they act as the
nested loops from your original post.

I guess you can see that the Action method is the one that you must
override in your derived classes. It corresponds to the methods
executed inside the most inner loop in the example you gave originally.

Now notice that the number of times Action will be executed is
exponencial to the number of levels, corresponding to ((Max - Min) + 1)
^ Levels. For instance, with Min=1, Max=10 and Levels = 10, the Action
method will be called 10^10 times, that is, 10000000000 times... =))

Regards,

Branco.
 
Thanks Branco for your explanation

Branco Medeiros said:
It's actually quite simple.

When you create an instance of a class derived from NestedLoop, it
initializes the loop parameters with a call to MyBase.New(). The class
must then call the protected method Execute, which will actually start
the loop by calling the private method DoLoop in the base class.

The DoLopp method just checks the current nest level: if it's 0 then it
calls the virtual method Action, otherwise it starts a new loop in the
specified range and calls itself recursivelly, decrementing Level as it
goes. This way there will be N recursive DoLoop calls, corresponding to
the number of levels you specified. For all effects, they act as the
nested loops from your original post.

I guess you can see that the Action method is the one that you must
override in your derived classes. It corresponds to the methods
executed inside the most inner loop in the example you gave originally.

Now notice that the number of times Action will be executed is
exponencial to the number of levels, corresponding to ((Max - Min) + 1)
^ Levels. For instance, with Min=1, Max=10 and Levels = 10, the Action
method will be called 10^10 times, that is, 10000000000 times... =))

Regards,

Branco.
 
To make my problem clear:

I want to have in a array (wich I can redim in runtime) of strings the
following
Choice to make in runtime :
Or "11","01","10",00
Or "111","110","101","100","011","010","001","000"
Or "1111","1110",1101","1100","1011",...............
..............
Or "111111111111","111111111110","111111111101",.....
.......
and so on
Is the method of Branco, wich I thank, the only solution?
 
That is you want to print out all binary numbers for a given length ?

If yes, a simple loop going from 0 to 2^length-1 will enumerate all possible
values. Then print out each value using the binary format...
 
andreas said:
I want to have in a array (wich I can redim in runtime) of strings the
following
Choice to make in runtime :
Or "11","01","10",00
Or "111","110","101","100","011","010","001","000"
Or "1111","1110",1101","1100","1011",...............
.............
Or "111111111111","111111111110","111111111101",.....
......
and so on
<snip>

In this case, maybe the following function works for you:

Function BinaryRange(ByVal Times As Integer) As String()
Dim Max As Integer = (2 ^ Times) - 1
Dim Result(Max) As String
For I As Integer = 0 To Max
Result(Max - I) = Convert.ToString(I, 2).PadLeft(Times, "0"c)
Next
Return Result
End Function

It retursns an array of strings built with the binary of the numbers
from (2^Times) - 1 down to 0, i.e., for Times = 4, it would be "1111",
"1110", "1101", "1100", "1011", "1010", "1001", "1000", "0111", "0110",
"0101", "0100", "0011", "0010", "0001" and "0000".

HTH.

Regards,

Branco.
 
Thanks to Branco and all the others
Branco, what you wrote is very simple and clever.
I feel a litle stupid, but thanks, thanks, thanks.8
 
Back
Top