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.
Printable View
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.
As far as I know a nested loop is nothing more than a loop within another loop...
this loop will print values from 0 to 9.Code:for(int i = 0; i < 10; i++)
printf("%d ", i);
now a loop within that loop
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.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);
}
Why are you using magic numbers?