Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

An Introduction to Python For Undergraduate Engineers/While Loops

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

While loops will repeat a section of code whilst a logical test is true. Instead of repeating something with a defined list of values or a set number of times, a while loop will happily continue repeating its section of code until a logical test is satisfied. Programs that run forever are to stop with Restart Shell in the IDLE or to hit the Control (or Ctrl) button and C (the letter) at the same time. For example: done = False # Creates a variable and gives it the logical vlue 'False' count = 0 # Creates a variable and gives it integer value 0 while done == False: # Repeats following code until logical test no longer satisfied count = count + 1 # Adds 1 to value of count variable print("The value of count is... " + str(count)) # Prints value of count The same code could also be written using != which means NOT EQUAL TO, for example: done = False count = 0 while done != True: count = count + 1 print("The value of count is... " + str(count))