Why does outer while-loop only execute once?

I have a nested while-loop. I want the outer loop to execute 5 times, and the inner loop to execute 5 times for each execution of the outer loop. However, when I run this code, the outer loop only executes once. I expected to see 25 console lines, but only the first 5 occur.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
unsigned int tempId = 1;
unsigned int maxId = 6;

unsigned int tempId2 = 1;

while ( tempId != maxId )
{
    while ( tempId2 != maxId )
    {
        std::cout << tempId << " , " << tempId2 << std::endl;

        ++tempId2;
    }

    ++tempId;
}
Last edited on
Try setting this line:
 
while(tempId<maxId)
That doesn't work either. Also, I used != because my actual loop conditions use iterators.

Nested for-loops work, so I've replaced my while-loops. However, I'm still interested in learning why the nested while-loops didn't work as expected.

P.S. I'm using Code::Blocks with the mingw32 compiler on Windows 10 64-bit.
@gentleguy
Stop trolling.

@carlwryker
Maybe use for loops since they are better for what you're using right now.

I'll address your problem now:
- Your while loops ARE working correctly but I'll explain why it seems the outer loop executes only once.
====================================================================
Here is what happens:

1. Your program starts executing outer loop and starts executing the loop inside.

2. Once tempId2 is equal to maxId because of the increment statement ++tempId2 it goes out of this inner loop.

3. Then it executes ++tempId;.

4. And NOW it executes outer loop over and over again BUT it does not execute the inner loop because now the inner loop evaluates to false (tempId2 != maxId).

Conclusion: So basically it execute outer loop, goes into inner loop and repeats it 5 times, and THEN repeats the outer loop 5 times but it is meaningless because nothing is executed in the outer loop after that. Therefore the console prints 5 lines and then prints none after that.

How I would write your code (this will print 25 lines as you want):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main()
{
unsigned int maxId = 6;

for (unsigned int tempId {1}; tempId != maxId; ++tempId)
{
    for (unsigned int tempId2 {1}; tempId2 != maxId; ++tempId2)
    {
        std::cout << tempId << " , " << tempId2 << std::endl;
    }
}
std::cin.ignore();
}


The reason the above works is because the ENTIRE inner for loop is executed meaning unsigned int tempId2 {1}; is executed every time the outer for loop repeats.

If you want to use your while loops you can put tempId2 = 1 right before or after ++tempId;

std::cin.ignore(); pauses.
Last edited on
@gentleguy
If you read the rules of this forum you wouldn't be saying that.

Anyways if you're not compiling with a C++11 compiler then it won't work. Or if your saying that it's a weird way to do it then I was just making it short. I was just using uniform initialization. (It compiled for me fine)

@carlwryker clearly just wants the 25 lines to be printed.
Last edited on
@gentleguy
You already know how old I am from another post.
And I use C++11 compiler because it supports uniform initialization in things that wasn't supported before.

EDIT: Just figured out I can compile with C++14 with my compiler I'll use that from now on.
Last edited on
Thank you!
@carlwryker
No problem!
Topic archived. No new replies allowed.