473,326 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

for next problem

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
13 1464
"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
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
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
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
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
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
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
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
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
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
2
by: Wonko | last post by:
Here's my problem if anyone could be so kind to help me out. I assume it's quite easy for an experienced programmer but I'm not one of them :) I have a JavaScript code that: - displays multiple...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
14
by: Ale K. | last post by:
i know that For...Next as a Exit For.... there is any way from the middle of my for...next code to go to the next item and jump out part of my code , like the same thing that can be done with exit...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
1
by: Tony Dunn | last post by:
I have inherited another developer's database (Access 2003) and with it a problem I'm struggling with. The problem is this. I have a parent-child form pair, which is populated by a table (the...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
0
by: solargovind | last post by:
Hello, I have few problem with Dlookup condition. I need to retrieve next record or previous record based on certain condition. The conditions are set in in the combo box. Here, I am trying to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.