Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 19 Online
OpenStudy (anonymous):

In section 3.2 of Prof. Guttag's book, the following code is shown: x=4 for j in range(x): for i in range(x): print i x = 2 It prints: 0 1 2 3 0 1 0 1 0 1 Can someone further explain to me what is going on with the for loops. The book explains that the "outer loop" is only evaluated once while the "inner loop is evaluated each time the inner for statement is reached". When exactly is the inner for reached? And why is it reached 3 times?

OpenStudy (unklerhaukus):

I am not sure what language that is composed in, but here is my interpretation for what is happening (notice the second declaration of x is only in the scope of the i-loop) x j x i PRINT = = = = === 4 0 4 0 0 4 0 4 1 1 4 0 4 2 2 4 0 4 3 3 4 1 2 0 0 4 1 2 1 1 4 2 2 0 0 4 2 2 1 1 4 3 2 0 0 4 3 2 1 1

OpenStudy (mathmate):

By the way, please do mention that you're working on Python. It helps. Yes, it was mentioned in the book that range is evaluated only once when the loop starts, and that's what Python does. So the situation is the following, annotating on @UnkleRhaukus 's outline: where J is the fixed limit of the loop j, and I is the fixed limit of loop i. \(\color{red}{J}\) j \(\color{red}{I}\) i PRINT (x) = = = = === 4 0 4 0 0 4 0 4 1 1 4 0 4 2 2 4 0 4 3 3 x=2 declared at the end of the first inner loop starting second round of inner loop with x=2 4 1 2 0 0 4 1 2 1 1 starting third round of inner loop with x=2 4 2 2 0 0 4 2 2 1 1 starting fourth round of inner loop with x=2 4 3 2 0 0 4 3 2 1 1 In C, a variable declared inside a loop has scope of the loop. In Python, the value of x declared inside the loop is visible outside the loop as well. To show this, you can add a print statement at the end of the outside loop and find that it prints a 2. x=4 for j in range(x): for i in range(x): print(i) x = 2 print(x) It prints: 0 1 2 3 0 1 0 1 0 1 \(\color{red}{2}\) notice that it prints a 2 instead of 4 outside the outer loop. (new versions of Python require parentheses around print items).

OpenStudy (e.mccormick):

Another way to do this is to walk through it verbally. ``` x=4 for j in range(x): for i in range(x): print i x = 2 ``` So what does that mean in words? `x=4` set x to 4. `for j in range(x):` create a range called j (0 to 3) based on x = 4 and use it in a for loop. `for i in range(x):` create a range called i (0 to 3) based on x = 4 and use it in a for loop. `print i` i = 0, so print 0 `x = 2` change x to 2 end of inner loop, i = 1, there is still more i `print i` i = 1, so print 1 `x = 2` change x to 2 end of inner loop, i = 2, there is still more i `print i` i = 2, so print 2 `x = 2` change x to 2 end of inner loop, i = 3 `print i` i = 3, so print 3 `x = 2` change x to 2 end of inner loop, there is no more i, finnish inner loop. end of outter loop, j = 1, there is still more j `for i in range(x):` create a range called i (0 to 1) based on x = 2 and use it in a for loop. `print i` i = 0, so print 0 `x = 2` change x to 2 end of inner loop, i = 1 `print i` i = 1, so print 1 `x = 2` change x to 2 end of inner loop, there is no more i, finnish inner loop. end of outter loop, j = 2, there is still more j `for i in range(x):` create a range called i (0 to 1) based on x = 2 and use it in a for loop. `print i` i = 0, so print 0 `x = 2` change x to 2 end of inner loop, i = 1 `print i` i = 1, so print 1 `x = 2` change x to 2 end of inner loop, there is no more i, finnish inner loop. end of outter loop, j = 3 `for i in range(x):` create a range called i (0 to 1) based on x = 2 and use it in a for loop. `print i` i = 0, so print 0 `x = 2` change x to 2 end of inner loop, i = 1 `print i` i = 1, so print 1 `x = 2` change x to 2 end of inner loop, there is no more i, finnish inner loop. end of outer loop, there is no more j, finnish outer loop. end of program

OpenStudy (e.mccormick):

The key part is when i and j are made they become independent of x. j is set to a range of 0, 1, 2, and 3 while x is 4. It is never set to anything else every again. That is what the book means by it being evaluated once. It is set then and run through one time in four steps. Then inner loop is evealuated each time the outter loop goes. The first time the x is still 4. That makes the first inner loop have 4 results when it is evaluated. However, it changes x to 2. That makes the other three times the inner loop is done it uses a range of 0 and 1 for i.

OpenStudy (anonymous):

Thank you all for your responses! I will be sure to indicate what language is being used for now on. So basically, the outer loop will be performed 4 times because x is initially set to 4. The inner loop will also be performed 4 times due to this initial condition, but after the first time it sees that x is now 2 and will print the appropriate results.

OpenStudy (e.mccormick):

Yes.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!