Download presentation
Presentation is loading. Please wait.
Published byVivien Moore Modified over 6 years ago
1
Iterations Programming Condition Controlled Loops (WHILE Loop)
2
Iterations – understanding WHILE loops
Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) Introduction We were introduced to iterations last lesson and saw how we could program a count controlled loop in python (FOR Loop). Today we shall look at another type of iteration. While loops will repeated whilst a certain condition is true. It is therefore known as a condition control loop. We shall now look to see how these can be programmed in Python.
3
While Loops in Python Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python While Loops are set up using the following statement: while x 0: Lets take a look more closely… Start X = 0 <> >= <= == > < != Execute Command These are called conditions Does ‘x’ = 5? No Yes
4
While Loops in Python ‘While Loops’ in Python while x n:
Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python while x n: 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 == != > < Start X = 0 Execute Command The n is represents a value that we want x to either equal, not equal, be greater than, etc. depending on the condition we want to use. E.g. n=5 and the condition while x != 5 (not equal to 5) then the loop would repeat until x equals 5. Does ‘x’ = 5? No Yes
5
While Loops in Python Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python x = 0 while x == 0: print(“Hello World”) Hello World Hello World Hello World Hello World Hello World Hello World Hello World
6
While Loops in Python ‘While Loops’ in Python x = 0 while x != 5:
Learning Objectives: To understand… …the use of the three basic programming constructs used to control the flow of a program: sequence selection iteration (count and condition controlled loops) ‘While Loops’ in Python x = 0 while x != 5: x = int(input(“Please type in a number”)) print(“Loop has ended”) Please type in a number: 1 Please type in a number: -5 Please type in a number: 122 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()) Please type in a number: 5 Loop has ended
7
Demonstrations
8
#Starting a line with a hash will turn your text into a comment.
Commenting #Starting a line with a hash will turn your text into a comment. (It will be ignored by the program)
9
Tasks WHILE LOOP TASKS: 1.Write a program that asks the user to input a series of numbers and adds them to a total until the user enters zero. (This stopping value is often called a rogue value). 2.Write a program which asks the user to set a password and then asks the user to enter the password again. Program it so that if the second password doesn’t match the one set, the request to enter the password is repeated until it does match the set password. 3.Write a program that asks the user for a number between 10 and 20 and validates (which means ‘tests’) that the input is within the correct range. It should repeatedly ask the user for a number from this range until the input is within the valid range.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.