Nested For loops

Hi All,

Trying to put together some nested For loops to output the following:

2,2,2
2,2,4
2,2,6
2,2,8
2,2,10
2,4,2
2,4,4
2,4,6
2,4,8
2,4,10
2,6,2
2,6,4
2,6,6
2,6,8
2,6,10
2,8,2
2,8,4
2,8,6
2,8,8
2,8,10
2,10,2
2,10,4
2,10,6
2,10,8
2,10,10
4,10,2
4,10,4
4,10,6
4,10,8
4,10,10
6,10,2
6,10,4
6,10,6
6,10,8
6,10,10
8,10,2
8,10,4
8,10,6
8,10,8
8,10,10
10,10,2
10,10,4
10,10,6
10,10,8
10,10,10

This is what I have so far and it starts off well but then goes off course:

int num = 2;
int x = 3;
const int d = 99;
  int k = num;
  int j = num;
  int i = num;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);  //Initialize the serial connection
  forLoopTest(num, x);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void forLoopTest(int num, int x) {
  for (k; k <= 10; k += 2 ) {
    Serial.print("1st Number: ");
    Serial.println(k);
    for (j; j <= 10; j += 2) {
      Serial.print("2nd Number: ");
      Serial.println(j);
      for (i; i <= 10; i += 2) {
        Serial.print("3rd Number: ");
        Serial.println(i);
      }
    }
  }
}

Here is the current output:

1st Number: 2
2nd Number: 2
3rd Number: 2
3rd Number: 4
3rd Number: 6
3rd Number: 8
3rd Number: 10
2nd Number: 4
2nd Number: 6
2nd Number: 8
2nd Number: 10
1st Number: 4
1st Number: 6
1st Number: 8
1st Number: 10

Any assistance would be greatly appreciated!

GorillaDetective:
Here is the current output:

That's what the program is telling it to do!

What do you want it to do that is different?

...R

for (k; Oops

Redundant but not oops.

.
.
.
int num = 2;
.
.
.
  int k = num;
.
.
.

(deleted)

In your desired output do you need the second number to stay at 10 for the rest of the sequence once it reaches 10? If so, you will have to have special logic to do this and simple nested for loops won't accomplish that.

vaj4088:
Redundant but not oops.

.

.
.
int num = 2;
.
.
.
int k = num;
.
.
.

You're right - I stand corrected.
Naff, though.

@spycatcher2k

Thank you for taking a look!

It is not an assignment I am way too old to be in school :slight_smile:

I am working on a project and need the program to loop through and generate these numbers in this order and pass each number to another function. This is just a simple test to get the logic correct.

In nested for loops, the innermost loop generally goes the "fastest" and the outermost loop generally goes the "slowest".

How could it be otherwise?

"Naff" is an uncommon word where I live but I agree.

@ToddL1962 Thanks for taking a look!

The desired output is exactly this:

2,2,2
2,2,4
2,2,6
2,2,8
2,2,10
2,4,2
2,4,4
2,4,6
2,4,8
2,4,10
2,6,2
2,6,4
2,6,6
2,6,8
2,6,10
2,8,2
2,8,4
2,8,6
2,8,8
2,8,10
2,10,2
2,10,4
2,10,6
2,10,8
2,10,10
4,10,2
4,10,4
4,10,6
4,10,8
4,10,10
6,10,2
6,10,4
6,10,6
6,10,8
6,10,10
8,10,2
8,10,4
8,10,6
8,10,8
8,10,10
10,10,2
10,10,4
10,10,6
10,10,8
10,10,10

I figured I am missing code or else I would have it working :wink:

Basically the idea is it should output every permutation of 3 groups of 10 incrementing by 2. (Hopefully that makes sense!)

It's naff, because i and j never get reinitialised (neither does k, but it isn't nested)

I didn't bother cleaning up the code I just made it work. This will give you that sequence:

int num = 2;
int x = 3;
const int dial = 99;
  int k = num;
  int j = num;
  int i = num;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);  //Initialize the serial connection
  forLoopTest(num, x);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void forLoopTest(int num, int x) {
  for (int k = num; k <= 10; k += 2 ) {
    for (int j = num; j <= 10; j += 2) {
      for (int i = num,; i <= 10; i += 2) {
        Serial.print(k);
        Serial.print(",");
        if (k <= num) Serial.print(j);
        else Serial.print("10");
        Serial.print(",");
        Serial.println(i);
      }
    }
  }
}

You have nothing to re-initialize the variables in the loops. You probably intended

.
.
.
  for (k=num; k <= 10; k += 2 ) {
.
.
.
    for (j=num; j <= 10; j += 2) {
.
.
.
      for (i=num; i <= 10; i += 2) {
.
.
.

k=num is not strictly necessary but looks consistent with the other loops. Besides, this is part of the habit that you should get into.

Thanks @ToddL1962! That worked great and was a clear example!

I cleaned it up a bit and added in the flexibility to adjust the increment amount and the "terminating number" (in this case 'd') easily. I also adjusted it so it will round down the output for the terminating number by 1 if the terminating number is odd as I want all even numbers. I think this works OK. I'd love a second set of eyes to check for any gotchas though...

Thanks!

int num = 2;
int x = 3;
const int d = 99;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);  //Initialize the serial connection
  forLoopTest(num, x);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void forLoopTest(int num, int x) {
  for (int k = num; k <= d; k += num ) {
    for (int j = num; j <= d; j += num) {
      for (int i = num; i <= d; i += num) {
        Serial.print(k);
        Serial.print(",");
        if (k <= num) Serial.print(j);
        else { if (d % 2 == 0) Serial.print(d);
        else Serial.print(d-1);
}
        Serial.print(",");
        Serial.println(i);
      }
    }
  }
}

Try using descriptive variable names. It makes the code more readable, and six months from now it will still make sense to you.

This works:

int outerLoop;
int midLoop;
int innerLoop;

void setup() {
  Serial.begin(115200);
  Serial.println();
  forLoopTest();
}

void loop() {
}

void forLoopTest() {
  for (outerLoop = 2; outerLoop <= 10; outerLoop += 2 ) {
    for (midLoop = 2; midLoop <= 10; midLoop += 2) {
      for (innerLoop = 2; innerLoop <= 10; innerLoop += 2) {
        //printf("%d,%d,%d", outerLoop, midLoop, innerLoop);
        Serial.print(outerLoop);
        Serial.print(F(","));
        Serial.print(midLoop);
        Serial.print(F(","));
        Serial.print(innerLoop);
        Serial.println();
      }
    }
  }
}

SteveMann:
Try using descriptive variable names. It makes the code more readable, and six months from now it will still make sense to you.

...but only give the variables the minimum necessary scope.

looks like you are not wanting a consistent pattern according to the output you say you want. in this situation nested loops won't help you. it would be easier to use one loop. then you can use some if's to set some rules for how you want your numbers to change. the code below gives you the pattern you posted.

		int a = 2; int b = 2; int c = 0;
	
		while (a+b+c<30) {
           if(c==10){c=0;if(b==10){a+=2;}
			if(b<10){b+=2;}}
			c+=2;
           
			// print a b c here
		}