By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,209 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,209 IT Pros & Developers. It's quick & easy.

for next problem

P: n/a
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
Jul 12 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"andreas" <an*****@pandora.beschrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jul 12 '06 #2

P: n/a
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)

--
Patrice

"andreas" <an*****@pandora.bea écrit dans le message de news:
wI***********************@phobos.telenet-ops.be...
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


Jul 12 '06 #3

P: n/a
yes, Herfried

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:#2**************@TK2MSFTNGP03.phx.gbl...
"andreas" <an*****@pandora.beschrieb:
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?

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 12 '06 #4

P: n/a
it is not possible to change the 4 times 10 loops in 40 loops because i do
something with i, j,k ...
"Patrice" <sc****@chez.comwrote in message
news:#1**************@TK2MSFTNGP04.phx.gbl...
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)

--
Patrice

"andreas" <an*****@pandora.bea écrit dans le message de news:
wI***********************@phobos.telenet-ops.be...
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


Jul 12 '06 #5

P: n/a
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...

--
Patrice

"andreas" <an*****@pandora.bea écrit dans le message de news:
3p***********************@phobos.telenet-ops.be...
it is not possible to change the 4 times 10 loops in 40 loops because i do
something with i, j,k ...
"Patrice" <sc****@chez.comwrote in message
news:#1**************@TK2MSFTNGP04.phx.gbl...
>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)

--
Patrice

"andreas" <an*****@pandora.bea écrit dans le message de news:
wI***********************@phobos.telenet-ops.be...
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




Jul 12 '06 #6

P: n/a
andreas wrote:
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.

Jul 12 '06 #7

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

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...
andreas wrote:
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.

Jul 12 '06 #8

P: n/a
andreas wrote:
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.

Jul 12 '06 #9

P: n/a
Thanks Branco for your explanation

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
andreas wrote:
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.

Jul 13 '06 #10

P: n/a
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?

"andreas" <an*****@pandora.bewrote in message
news:wI***********************@phobos.telenet-ops.be...
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


Jul 13 '06 #11

P: n/a
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...

--
Patrice

"andreas" <an*****@pandora.bea écrit dans le message de news:
G%***********************@phobos.telenet-ops.be...
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?

"andreas" <an*****@pandora.bewrote in message
news:wI***********************@phobos.telenet-ops.be...
>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



Jul 13 '06 #12

P: n/a

andreas wrote:
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.

Jul 13 '06 #13

P: n/a
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

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>
andreas wrote:
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.

Jul 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.