Trace The Nested "For" Loops

for i in range(2, 5):
for j in range(4, 9):
print(i, j)

Trace the code above in the Python Visualizer. How many times is p r i n t ( i , j ) print(i, j) executed?

1) 24 24
2) 3 3
3) 15 15
4) 5 5

4 3 2 1

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

13 solutions

Discussions for this problem are now closed

Ahaan Rungta
Dec 22, 2013

The answer is 0 0 because the indentation is faulty. But assuming the correct indentation, we note that we encounter 3 3 values of i i and 5 5 values of j j , so the answer is 3 5 = 15 3 \cdot 5 = \boxed {15} .

I don't know python but I know java ...

Tell me why is it not 24 ... I mean i takes values from 2 to 5 and j from 4 to 9

we actually get 4*6

Santanu Banerjee - 7 years, 5 months ago

In Python the range function works as follows.

range (x, y) , for integral x, y and y x y \ge x considers all the integral values from x x to y 1 y - 1 , inclusive. So:

range (2, 5) considers 2 , 3 , 4 2, 3, 4 and range (4, 9) considers 4 , 5 , 6 , 7 , 8 4, 5, 6, 7, 8 . Thus, it considers y x y - x values, not y x + 1 y - x + 1 values.

Ahaan Rungta - 7 years, 5 months ago

"for i in range(2, 5)" means i [ 2 , 4 ] i\in[2, 4] and it is an integer. Therefore, there are 4 2 + 1 = 3 4-2+1=3 possibilities for i i . "for j in range(4, 9)" means j [ 4 , 8 ] j\in[4, 8] and it is an integer. Thus, there are 8 4 + 1 = 5 8-4+1=5 possibilities for j j . Therefore, p r i n t ( 1 , j ) print(1, j) is executed 3 5 = 3 ) 15 3\cdot5=\boxed{3) 15}

Prasun Biswas
Dec 21, 2013

This is an example of nested loops where i is counter of outer loop and j is counter of inner loop. The outer loop runs 3 times with i=2,3,4 and corresponding to each execution of outer loop, the inner loop gets executed 5 times with j=4,5,6,7,8. print(i,j) is within the inner loop. So, total no. of times print(i,j) gets executed = ( 3 × 5 ) = 15 =(3\times 5) = \boxed{15}

Aditya Bhatnagar
Feb 3, 2014

Answer: 3) 15.

For 'i' in range (2,5) means that the loop will run if 'i' is 2, 3, or 4. Hence, 3 times. Now, the second loop will run 5 times as the value of 'j' should be in the range of (4,9) that is, 4, 5, 6, 7 or 8.

Now the nested loop [for j in range(4,9)] will run 5 times in each of the three instances of the first loop [for i in range(2,5)]. Ergo, resulting in total 5*3 = 15 executions of print(i,j)

r a n g e ( 2 , 5 ) range(2, 5) iterates 3 values: 2, 3, and 4

r a n g e ( 4 , 9 ) range(4, 9) iterates 5 values: 4, 5, 6, 7, and 8

Therefore there are 3 × 5 = 15 3 \times 5 = 15 combinations of i i and j j

Madhu Steve
May 21, 2014

for i it takes 3 num and for j it takes 5 num so 5*3=15

Nirob 3.1416
Apr 5, 2014

I have no idea about python language . But when i tried to compile the code given on the problem statement , following warning showed up : 'IndentationError: expected an indented block (<string>, line 2) ' . I didn't hear anything like 'IndentationError:' in c or cpp language .

Vaibhav Zambad
Mar 20, 2014

c=0 for i in range(2, 5): for j in range(4, 9): c=c+1 print c

in the above python code the value of c gives the answer..

Saddam Ranjhani
Mar 12, 2014

Its So Simple Actually It should be told that the python includes the range values or excludes it, here situaltion is bit differnet a range in python works like this as (4,9) means 4 is inclusive while 9 exclusive :) so it will iterate 4,5,6,7,8

Alekh Raj
Jan 5, 2014

in range(a,b) :iteration takes place from a(included) to b(not included)..

Pebrudal Zanu
Jan 4, 2014

Python will read set formed by ( 2 , 3 , 4 ) × ( 4 , 5 , 6 , 7 , 8 ) (2,3,4) \times (4,5,6,7,8)

The answer 3 × 5 = 15 3 \times 5=\fbox{15}

Raghav Dua
Dec 30, 2013

Write the script for a python compiler and declare a variable 'counter' at first. write 'counter = counter + 1' below 'print (i, j)' and write 'print counter' in the script's end.

Isak Falk
Dec 22, 2013

This is actually more of a counting problem. Once inside the first for- loop, we see that the program will iterate over 3 3 objects (due to syntax: 2 , 3 , 4 2, 3, 4 ). For each of the objects we go on to the next step and see that the for- loop at this level is iterated over 5 5 times ( 4 , 5 , 6 , 7 , 8 4, 5, 6, 7, 8 ) executing the print statement for each object. Thus the print statement will be executed 3 5 = 15 3*5 = \boxed{15} times.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...