3 Replies - 1533 Views - Last Post: 11 November 2016 - 05:20 AM Rate Topic: -----

#1 *Jas*  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 29-February 08

while loops..

Posted 09 March 2008 - 12:13 PM

How many times will python execute the code inside the while loops,below?


i = 1
while i < 10000 and i > 0 and 1 :
print "still going ..."
i = 2 * i





i = 1
while i < 10000 and i > 0 and 0 :
print "still going ..."
i = 2 * i





i = 1
while i < 2048 and i > 0 :
print "still going ..."
i = 2 * i




Is This A Good Question/Topic? 0
  • +

Replies To: while loops..

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: while loops..

Posted 09 March 2008 - 11:37 PM

hmmm..... odd questions you have.
Was This Post Helpful? 0
  • +
  • -

#3 umtabaldi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-November 16

Re: while loops..

Posted 11 November 2016 - 03:39 AM

View Post*Jas*, on 09 March 2008 - 12:13 PM, said:

How many times will python execute the code inside the while loops,below?


i = 1
while i < 10000 and i > 0 and 1 :
print "still going ..."
i = 2 * i





i = 1
while i < 10000 and i > 0 and 0 :
print "still going ..."
i = 2 * i





i = 1
while i < 2048 and i > 0 :
print "still going ..."
i = 2 * i



12

This if executed well
I did with I python jupytr notebook
Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 683
  • Joined: 27-December 13

Re: while loops..

Posted 11 November 2016 - 05:20 AM

You do not need the i > 0 condition; i starts as 1 and is multiplied with a positive number.
Also and 1 and and 0 will always evaluate to True rsp. False
So, you first code will print 14 times (i values 1, 2, 4, 8, .. , 8192)
The second code will not print at all.
The last code, - I'll leave it to you...

I assume the missing indents are only misprints, you need to indent the code block below the while statement.

EDIT: Sorry, I only now see that this is really a very old thread. I guess this in no longer relevant!

This post has been edited by DK3250: 11 November 2016 - 05:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1