Public Group

#### Archived

This topic is now archived and is closed to further replies.

# for loop inside a for loop

This topic is 5232 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

// historgram pringtin program

#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

int main()
{
const int arraySize = 10;
int n[ arraySize ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };

cout << "Element" << setw( 13 ) << "Value"
<< setw( 17 ) << "Histogram" << endl;

for ( int i = 0; i < arraySize; i++ ) {
cout << setw( 7 ) << i << setw( 13 )
<< n[ i ] << setw( 9 );

for ( int j = 0; j < n[ i ]; j++ ) // print one bar

cout << ''*'';

cout << endl;
}

return 0;
}

is int j ever increased? because if it does increase, how does it print the x number of asterisks in the array n[]? and, I know that in a for loop, the the third test/condition is checked last, but in this nested for loop, after i < arraySize and the two statements, does it go offto the second for loop? so basically when do i++ and j++ increase? thanks !

##### Share on other sites
the easiest way to figure this out for yourself is to run the code using a debugger and step through the program. You will then see exactly what happens at every point during execution.

##### Share on other sites
On each iteration of the i-for ALL the iterations of the j-for are done. When the j-for is done, the endl is printed and then i is increased. The next iteration of the i-for then begins.

This:

for (int i = 0; i < x; i++) {
...
...
}

Is the same as:

int i = 0;
while (i < x) {
...
...
i++;
}

##### Share on other sites
quote:
Original post by Blew
On each iteration of the i-for ALL the iterations of the j-for are done. When the j-for is done, the endl is printed and then i is increased. The next iteration of the i-for then begins.

This:

for (int i = 0; i < x; i++) {
...
...
}

Is the same as:

int i = 0;
while (i < x) {
...
...
i++;
}

Actually, it isn''t the same, because the former example (using for loop) the variable i only has scope within the loop, wheras in the while example it has scope beyond the loop.

##### Share on other sites
"i" and "j" increase on every iteration. But, the way you have it set up is that j increases by one to the amount contained in the index of array.

so on the first iteration,

i = 0;
j = 0;

j increments to 19, because array(i) holds 19.

the whole thing would loop throuh 10 times. after "j" is finished, the program flow goes back up the "i" and then "i" is incremented, at which point it goes back through and then a new "j" is created and assigned a value of 0. then it loops through 0 to 3, because array(i) holds 3.

[edited by - MrBeaner on February 27, 2004 1:04:17 AM]

##### Share on other sites
quote:
Original post by RDragon1
Actually, it isn''t the same, because the former example (using for loop) the variable i only has scope within the loop, wheras in the while example it has scope beyond the loop.

Here goes the 1.1 patch:

This:

for (int i = 0; i < x; i++) {
...
...
}

Is the same as:

{
int i = 0;
while (i < x) {
...
...
i++;
}
}

##### Share on other sites
Like I said, use a debugger and run it, then watch how i and j increase. Not only will it very easily answer your question a lot clearer than explaining it in words, but it will also give you an exercise in using a debugger if you don''t know how to use one already.

##### Share on other sites
quote:
Original post by MrBeaner
"i" and "j" increase on every iteration. But, the way you have it set up is that j increases by one to the amount contained in the index of array.

so on the first iteration,

i = 0;
j = 0;

j increments to 19, because array(i) holds 19.

the whole thing would loop throuh 10 times. after "j" is finished, the program flow goes back up the "i" and then "i" is incremented, at which point it goes back through and then a new "j" is created and assigned a value of 0. then it loops through 0 to 3, because array(i) holds 3.

[edited by - MrBeaner on February 27, 2004 1:04:17 AM]

haha, thanks man. perfect explanation.

i tried using the debugger but i have no idea how. i dont know what to do with the step in, step out buttons....ill find a tutorial how though.
thanks

##### Share on other sites
quote:
Original post by TheOne1
...ill find a tutorial how though.

I recommend you do that as a priority. Don''t wait until you are almost blowing your head off to give a try at a debugger. Do it while you still have your sanity. Lose it afterwords, as a professional. haha

##### Share on other sites
Just be aware that in the case of Step In / Step Over, neither skips any code, it is basically used when at a source line that calls a function, if you Step Over, it executes code until execution reaches the next line in the file you are currently viewing, while on the other hand Step In will step into the function that gets called.

1. 1
Rutin
19
2. 2
3. 3
4. 4
5. 5
frob
12

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000104
×