How are the results for count different in all these three cases?

1.3k views Asked by At

Code 1:

iteration = 0

count = 0

while iteration < 5:

    for letter in "hello, world":

        count += 1

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1

Code 2:

iteration = 0

while iteration < 5:

    count = 0

    for letter in "hello, world":

        count += 1

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1

Code 3:

iteration = 0

while iteration < 5:

    count = 0

    for letter in "hello, world":

        count += 1

        if iteration % 2 == 0:

            break

    print "Iteration " + str(iteration) + "; count is: " + str(count)

    iteration += 1
3

There are 3 answers

3
Henry Zhu On BEST ANSWER

For Code 1, you're continuing to add on to the count. So during the first iteration, the count becomes 12 (the length of "hello, world" is 12), and then during the second iteration, you never reset the count to 0 so count is continuously added on until it reaches 24, as it adds on the length of "hello, world" again (12 + 12 = 24).

0 + len("hello, world") = 12

12 + len("hello, world") = 24

and so forth

For Code 2, count is reset to 0 each time. This means that the count will always equal 12, as the length of "hello, world" is 12.

0 + len("hello, world") = 12

reset to 0 

0 + len("hello, world") = 12

and so forth

For Code 3, you break every time the iteration is an even number. That means for iterations 0, 2, and 4, iteration returns a value of 1 as 1 is added to iteration at the beginning of the for loop. But during odd iterations, the count is 12, since the program does not break out of the for loop and adds the length of "hello, world", which is 12.

count = 0

For loop
     "h" in "hello, world"

     Add 1, 0 -> Is it even or odd? Even, Do not add len("ello, world") to count

1

count = 0

For loop
     "h" in "hello, world"

     Add 1, 0 -> Is it even or odd? Odd, Do add len("ello, world") to count

12
0
Leb On

Code 1:

Your count is set outside the while loop, therefore it is not effected by it and thus increase by 12 len(hello, world) = 12

Code 2:

Your count is inside the while loop, therefore it resets with each iteration ONLY. That's why count = 12 is staying the same while Iteration, being outside the loop, increases.

Code 3:

When your Iteration is even the code is breaking after the first count. When it's odd it runs through the code fine.

1
Rakholiya Jenish On

In code 1 during every iteration, you are adding value of count to the previous one. While in the code 2, at the beginning of every iteration, you are re-assigning the count = 0. In each iteration, after the for loop is executed, 12 is added to the previous value of count which in case of code 2 is always 0. Hence value of count is different in both the case.

In case 3, after the execution of for loop, either you count value will be 1 (if the value of iteration is even) or 12 (if the value of iteration is odd). This is because of the if condition that check for the i%2. Hence in case 3, value of count is different for odd and even. And since during each iteration you are reassigning count = 0, value of count for all odd number is 12 and value of count for all even number is 1.