Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1:...
Transcript of Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1:...
![Page 1: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/1.jpg)
Programming for GCSE Topic 4.1: While Loops
Teaching London Computing
William Marsh School of Electronic Engineering and Computer Science
Queen Mary University of London
![Page 2: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/2.jpg)
Aims
• Introduce the 'while' loop • Consider the sequence of ideas • Repeat a statement, a fixed number of times • Repeat with variation, a fixed number of times • Repeat until you succeed, varying number of times
![Page 3: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/3.jpg)
WHILE LOOPS
![Page 4: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/4.jpg)
Simple ‘While’ Statement
• Execute statements repeatedly
counter = 1 !while counter <= 5:! print("Inside the loop")! counter = counter + 1 !
Key word Condition
: indentation
![Page 5: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/5.jpg)
Boxes
• A ’while’ statement has an inside
while condition A:! !Renter the box while 'A’ is true
![Page 6: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/6.jpg)
Using a Counter
• Initialise, test, increment
counter = 1 !while counter <= 5:! print("Inside the loop")! counter = counter + 1 !
initialise
increment
test
![Page 7: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/7.jpg)
Quiz: Counter Values
• What is the sequence of counter values?
counter = 1 !while counter <= 5:! print("Inside the loop")! counter = counter + 1 !
![Page 8: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/8.jpg)
Quiz: Counter Values counter = 1 !while counter <= 5:! print("Inside the loop")! counter = counter + 1 !
Counter Print 1 "Inside the loop" 2 "Inside the loop" 3 "Inside the loop" 4 "Inside the loop" 5 "Inside the loop" 6 -
![Page 9: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/9.jpg)
Quiz: Counter Values
• How many lines printed?
counter = 7 !while counter <= 15:! print("Inside the loop")! counter = counter + 2 !
![Page 10: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/10.jpg)
Quiz: Counter Values
counter = 7 !while counter <= 15:! print("Inside the loop")! counter = counter + 2 !
Counter Print 7 "Inside the loop" 9 "Inside the loop" 11 "Inside the loop" 13 "Inside the loop" 15 "Inside the loop" 17 -
![Page 11: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/11.jpg)
Varying the Code Repeated
• Previously, same code repeated • Use counter to make it vary
counter = 1 !while counter < 6:! print(”The counter is”, counter)! counter = counter + 1 !
![Page 12: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/12.jpg)
Varying the Code Repeated counter = 1 !while counter < 6:! print(”The counter is”, counter)! counter = counter + 1 !
Counter Print 1 The counter is 1 2 The counter is 2 3 The counter is 3 4 The counter is 4 5 The counter is 5 6 -
![Page 13: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/13.jpg)
Varying the Code Repeated cntr = 1 !while cntr < 6:! if cntr % 2 == 0 :! print(”The counter is”, cntr)! cntr = cntr + 1 !
Counter Print 1 2 The counter is 2 3 4 The counter is 4 5 6 -
![Page 14: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/14.jpg)
Order Matters
• These two programs differ • Can you explain how?
counter = 1 !while counter < 6:! print(”The counter is”, counter)! counter = counter + 1 !
counter = 1 !while counter < 6:! counter = counter + 1 ! print(”The counter is”, counter)!
![Page 15: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/15.jpg)
Errors
• This program looks similar but it does not work • Can you see why?
cntr = 1 !while cntr < 6:! if cntr % 2 == 0 :! print(”The counter is”, cntr)! cntr = cntr + 1 !
![Page 16: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/16.jpg)
Counter Loops – Summary
• Counter • Initialise it • Test it • Change it
• The counter has a sequence of values in the loop body • The counter value can be used to vary the
statement executed each time through the loop
![Page 17: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/17.jpg)
COMPARISONS
![Page 18: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/18.jpg)
Loops in Scratch
• Scratch has 4 loops
Loop without end
Loop fixed number of time
Condition
Loop while condition true
Loop until condition true
Condition
![Page 19: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/19.jpg)
’While’ Language
• Many instructions have ‘loop’ language
Put the butter, sugar, fruit, zests, juice and 100ml/3½fl oz brandy in a large pan. Bring slowly to the boil, stirring until the butter has melted. Reduce the heat and bubble for 10 minutes, stirring occasionally.
while not (butter melted) :! stir! pause!
![Page 20: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/20.jpg)
WHILE WITH CONDITION
Not all loops have counters
![Page 21: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/21.jpg)
Loop with a Condition
• The number of iterations can vary
needAnswer = True!while needAnswer :! ans = input(”Answer Yes or No! ")! if ans == “Yes” or ans == “No”:! needAnswer = False !
Answer Yes or No! Not sure Answer Yes or No! Ok Answer Yes or No! Will do Answer Yes or No! I said alright Answer Yes or No! Yes
![Page 22: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/22.jpg)
Childhood Classic
print(“I can spell Mississippi”)!goes = 0!ans = -1!while ans != 0 :! ans = int(input(”How many ‘s’ in it?”))! goes = goes + 1 !!print(“You answered in”, goes, “goes”)!
![Page 23: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/23.jpg)
TEACHING ISSUES
![Page 24: Programming for GCSE Topic 4.1: While Loops · 04-05-2014 · Programming for GCSE Topic 4.1: While Loops T eaching L ondon C omputing William Marsh School of Electronic Engineering](https://reader033.fdocuments.in/reader033/viewer/2022042218/5ec476a895fe5563720f5db0/html5/thumbnails/24.jpg)
Teaching Issue
• How many stages for loops • Counter: repeating the same statement • Counter: varying the statement • Vary number of iterations
• Python has other loops (like Scratch) • Are they needed?
• Loops are the most complex aspect of programming