1 / 7

While Loops (Iteration 2)

While Loops (Iteration 2). Programming Guides. While Loops (Iteration 2). Think about when you log in… “While the password you enter, doesn’t equal the password on your account, the program will repeatedly ask you to enter your password.” This is an example of a while loop in action!.

beckamy
Download Presentation

While Loops (Iteration 2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. While Loops (Iteration 2) Programming Guides

  2. While Loops (Iteration 2) Think about when you log in… “While the password you enter, doesn’t equal the password on your account, the program will repeatedly ask you to enter your password.” This is an example of a while loop in action!

  3. While Loops (Iteration 2) < == > != <> >= <= These are called conditions ‘While Loops’ in Python While Loops are set up using the following statement: while x 0: Lets take a look more closely…

  4. While Loops (Iteration 2) The x is simply a variable. It could have any name. It is however a special kind of variable known as the ‘most recent value’ We must finish the statement with a colon == != > < The n is simply representing a value that we want x to either equal, not equal, be greater than etc depending on the while loop condition. If n=5 and the condition was while x != 5 (not equal to 5) then the loop would repeat until x equals 5. ‘While Loops’ in Python while xn:

  5. While Loops (Iteration 2) ‘While Loops’ in Python Hello World Hello World Hello World Here is an example of a while loop that will never end. Because there is never a situation when x can change, the loop will repeat forever. Hello World Hello World Hello World Hello World x = 0 while x == 0: print(“Hello World”)

  6. While Loops (Iteration 2) ‘While Loops’ in Python Remember: If you create a condition where a variable is being checked against an integer, you must remember to convert the variable’s input into an integer (e.g. int()) 5 -5 122 1 0 1 Please type in a number: -5 Please type in a number: 122 Please type in a number: Here is an example of a while loop that will stop. Because the statement in the loop is asking for a new value for x, it means that x can be changed and so can stop if the correct value is entered. 5 Please type in a number: Loop has ended x = 0 while x != 5: x = int(input(“Please type in a number”)) print(“Loop has ended”)

  7. While Loops (Iteration 2) Example of a while loop in action:

More Related