Im currently taking my first c++ class and i am having a very difficult time understanding nested loops. Could someone walk me thru it? how does the inner loop relate to the outer loop?
for (int i = 1; i<=n; i++)
{
for ( int j = 1; j<= i; j++)
cout << "*";
cout << "\n";
}
first i is initialized with 1,the condition i<=n verifies and then comes second for.....j is initialized with 1 and second for ends when the condition j<=i isn't true...........then it prints "\n" and comes to first for and i gets 2, then verifies whether i<=n conition is true....if true then it passes to second for...........
In order for the outer-loop to complete a full loop, the inner-loop must must complete all of it's loops, or break from the loop( whichever comes first ).
A more visual way of how the code executes can be seen by using the Visual Studio debugger. It allows you to step through the code line-by-line so you can actually see what's happening. (That's how I usually taught the control structures to the students that I tutored.)