Trace through nested for loop

I am looking for help with tracing through a program (not certain the terminology to use there...) to help me understand nested for loops. Below is a sample program with results from my textbook, and some of my failed trace attempts.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
using namespace std;

int main()
{
  int lastNum;
  int numToPrint;

  for (lastNum = 1; lastNum <= 7; lastNum++)
   {
     for (numToPrint = 1; numToPrint <= lastNum; numToPrint++)
     cout << numToPrint;
     cout << endl;
    }
 return 0;
}


Result:
1
12
123
1234
12345
123456
1234567

Trace Attempt 1:
lastNum starts as 1
lastNum(1) meets condition of <= 7
Enter nested loop
numToPrint starts as 1
numToPrint(1) meets condition of <= lastNum(1)
Print numToPrint(1)
Linefeed
numToPrint is incremented to 2.
lastNum is incremented to 2
lastNum(2) meets condition of <=7
Enter nested loop.
numToPrint(2) meets condition of <= lastNum(2)
Print numToPrint(2)
Linefeed

Trace Attempt 2:
lastNum starts as 1
lastNum(1) meets condition of <= 7
Enter nested loop
numToPrint starts as 1
numToPrint(1) meets condition of <= lastNum(1)
Print numToPrint(1)
Linefeed
numToPrint is incremented to 2.
numToPrint(2) does not meet condition <= lastNum(1)
Do not print numToPrint
Do not print linefeed
lastNum is incremented to 2
...and now I'm just back to the first trace attempt.

Trace Attempt 3:
lastNum starts as 1
lastNum(1) meets condition of <= 7
Enter nested loop
numToPrint starts as 1
numToPrint(1) meets condition of <= lastNum(1)
Print numToPrint(1)
linefeed
increment numToPrint(2)
increment lastNum(2)
numToPrint(2) meets condition of <= lastNum(2)
print numToPrint(2)
linefeed


I know I'm doing something wrong with tracing through the loop, but I can't figure out my misstep. I appreciate the help. Thank you!
Each time when nested loop is executed numToPrint starts from 1 again.
Would it be possible for you to trace through the code for one or two loops? I am still not understanding how the cout of numToPrint followed by an endl can result in more than one number on a row. I must not be executing the loops correctly. Thank you.
1) your identation is slightly wrong and can confuse you. I corrected it:
1
2
3
4
5
for (lastNum = 1; lastNum <= 7; lastNum++) {
     for (numToPrint = 1; numToPrint <= lastNum; numToPrint++)
         cout << numToPrint; //Part of inner loop
     cout << endl; //Not a part of inner loop, it belongs to the outer loop
}


set lastNum to 1
condition (lastNum (1) <= 7) is passed
enter nested loop
    set numToPrint to 1
    condition (numToPrint (1) <= lastNum (1)) is meet
    print numToPrint (1)
    increment numToPrint 
    start nested loop again:
    condition (numToPrint (2) <= lastNum (1)) is not meet
    exit nested loop
print linefeed
increment lastNum
start outer loop again:
enter nested loop
    set numToPrint to 1
    condition (numToPrint (1) <= lastNum (2)) is meet
    print numToPrint (1)
    increment numToPrint 
    start nested loop again:
    condition (numToPrint (2) <= lastNum (2)) is meet
    print numToPrint (2)
    increment numToPrint 
    start nested loop again:
    condition (numToPrint (3) <= lastNum (2)) is not meet
    exit nested loop
print linefeed
increment lastNum
start outer loop again:
//...
Huge help. Thank you so much. Knowing that the endl is part of the outer loop enables me to understand the program. I thought I had learned that indentation was just for clarity and that it was the braces that dictated what happened within a loop...confusing for a beginner for this not to be the case...and helpful to know where I was getting confused. Again, thank you very very much.
Identation is just for clarity. Bad identation confused you and proper identation helped to understand where newline output belongs.
This is why identation important: wrong identation can mislead you.
I still don't understand how the braces are working then, I guess. I would have thought everything within the braces would run before returning to the start of the outer loop.
I would have thought everything within the braces would run before returning to the start of the outer loop.
It does. It is the inner loop working which you did not get. If there is no braces after control statement (lke if/while/for etc.), it will threat only next "line" as related to it, i.e. threat next line as if it was in braces:

1
2
3
4
5
6
for (lastNum = 1; lastNum <= 7; lastNum++) {
     for (numToPrint = 1; numToPrint <= lastNum; numToPrint++) {
         cout << numToPrint; //Part of inner loop
     }
     cout << endl; //Not a part of inner loop, it belongs to the outer loop
}
you need to put this code after your second for:cout<<\n
to goes in a new line for each time
like this
for()
{
for()
{

}
\n
}
if you trace your program it show this:
112123123412345...
Last edited on
Hi, the best way for you to trace your code is to use your debugger. :)

The debugger actually shows you each line of code as it gets executed - perfect for understanding the flow of execution.

Joe
Concord Spark Tutor
sparkprogrammer@gmail.com
If I had a debugger...I'm very new to programming. I am connecting to a remote Linux server to program. I don't know if that complicates my options. Feeling a wee bit overwhelmed at the moment... In any event. thanks for the input - I do appreciate it.
Depends on what IDE you're using.

If you want more info, email me at: sparkprogrammer@gmail.com
Topic archived. No new replies allowed.