How Many Times Will This While Loop Execute?

How many times will this while loop (in Python) execute?

1
2
3
4
i = 1
while i % 2 != 0:
    print "We are looping"
    i += 2

0 1 2 Infinite

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.

1 solution

Sam Bealing
Jun 28, 2016

Relevant wiki: While

The condition for the while loop to continue is that i ≢ 0 ( m o d 2 ) i 1 ( m o d 2 ) i \not \equiv 0 \pmod{2} \implies i \equiv 1 \pmod{2} .

Initialy i = 1 i=1 and each time i i is set to i + 2 i+2 this means i 1 ( m o d 2 ) i \equiv 1 \pmod{2} so the loop will continue Infinitely \boxed{\boxed{\text{Infinitely}}}

Moderator note:

Simple standard approach.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...