When I am trying to design an algorithm , I had a programming problem in Java ..
I want to write M nested "for" like that:

``````for (I[0]=1; I[0]<=N; I[0]++)
for (I[1]=1; I[1]<=N; I[1]++)
...
for (I[M]=1; I[M]<=N; I[M]++)
{
Statements;
}
``````

Where M is entered by the user during the running of the program
Is there a possible way to do it ? ( even in any programming language )

4
Contributors
11
Replies
13
Views
5 Years
Discussion Span
Last Post by vriend12345

Are you asking if the source code for a program can be changed while the program is executing?
The number of nested loops would be specified in the source before it is compiled.

What problem are you trying to solve?

Basically you are doning 1...N permutation of max N repeats. There is ways, like recursion to one less loops until at zero you have each value doing the statements, but probably you need to copy the array before the recursive call. And as you mention any language it could not be simpler in Python without recursion with itertools module:

``````from itertools import product

n = int(raw_input('Give count of numbers: '))
for p in product(range(1,n+1), repeat=n):
print('statement %s' % (p,))
``````

I am not asking that if I can change the source code while the program is executing ..
I know that is not possibe ..
But I want a code that make me nest M for ..
where M is a variable number while the program is executing ..

I am trying to solve a research problem in Computational Algebra ..

Thank you very much pyTony ..
unfortunately Python is not of my skilled programming language ..
but I will try to find an equivalent code to yours in Jave ..

Edited by vriend12345

What do you mean by "nest M"? Your posted example showed source code.
How do you set/change nesting levels of for loops at execution time?

What problem are you trying to solve?

I mean I want to nest M numbers of for loop
I am trying to test all the matrices of the dimension NxN where the elements are from Z_N to whether it verify a condition

You can't change the compiled code. You'll need another algorithm, like recursion.

Thank you ..

Is this solved, or would you like more help with the recursive solution?

Thank you JamesCherrill ..
It's still unsolved ..
The recursive solution is not prefered because of its complexity ..
I am trying to re-design the algorithm to exclude this problem ..

It's really not complex, once you wrap your brain around the concept of recursion. Here's a generic variable nested loop (using n and m as in your example)

``````   void recursiveNestedLoop(int depth) { // make first call with depth=0
if (depth > m) return;
for (int i = 0; i < n; i++) {
recursiveNestedLoop(depth + 1);
}
}
``````

... and here's an example that keeps a listing of how it has recursed and prints the combinations that it is processing

``````   private void loop(int depth, String soFar) {
// soFar builds up a displayable stack of the recursive calls
if (depth > m) {
// only executed "inside" the inner-most loop
System.out.println(soFar);
return;
}
for (int i = 0; i < n; i++) {
loop(depth+1, soFar + " " + i);
}
}
``````

... you can call that with something like

``````      int n=4;
int m=3;
loop(0, "");
``````

Thank you JamesCherrill ..

I meant by "complexity" the idiom known in computational complexity theory That means the number of steps that is done to solve a problem ..

This code is worked perfectly for small values of m,n and it would help vey much in any same problem ..
for a larger values I will research for an other algorithm ..

Thank you so much JamesCherrill ..
Thank you NormR1, pyTony ..