# Help with nested loops(im a new programmer)

• 06-28-2010
cuo741
Help with nested loops(im a new programmer)
Hello,i just recently started trying to learn C++ and i got to nested loops and it got me confused, so if you could give me example on nested loops and explain what it does and how it does it,if you can, that would be most appreciated thank you.
• 06-29-2010
vart
Quote:

Originally Posted by cuo741
Hello,i just recently started trying to learn C++ and i got to nested loops and it got me confused, so if you could give me example on nested loops and explain what it does and how it does it,if you can, that would be most appreciated thank you.

Do you understand what the not-nested loop does?
• 06-29-2010
Dante Wingates
As far as I know a nested loop is nothing more than a loop within another loop...

Code:

for(int i = 0; i < 10; i++)
printf("%d ", i);

this loop will print values from 0 to 9.

now a loop within that loop

Code:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main(void)
{
for(int i = 0; i < 10; i++){
printf("%d: ", i);
for(int j = 0; j < 10; j++)
printf("%d ", j);
std::cout.put(0xA);
}

getchar();
return(0);
}

The first loop will execute 10 times. This will execute the inner loop each time the first loop executes, and the inner loop prints the values from 0 to 10 on the screen each time it is executed. In other words it will print 0 to 10 ten times. When control reaches the inner loop, it enters in a loop and only passes control to the first for loop once the variable j is not lower than ten, and then it repeats.
• 06-29-2010
Elysia
Why are you using magic numbers?