anonymous
  • 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?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
UnkleRhaukus
  • 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
mathmate
  • 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).
e.mccormick
  • 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

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

e.mccormick
  • 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.
anonymous
  • 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.
e.mccormick
  • e.mccormick
Yes.

Looking for something else?

Not the answer you are looking for? Search for more explanations.