While Loops in Programming for GCSE Students

t eaching l ondon c omputing n.w
1 / 24
Embed
Share

Discover the concept of while loops in programming, including how to repeat statements a fixed number of times or until a condition is met. Learn through examples and quizzes to solidify your understanding. Perfect for GCSE students studying computing.

  • Programming
  • Loops
  • GCSE
  • Education
  • London

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. TeachingLondon Computing Programming for GCSE Topic 4.1: While Loops William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

  2. 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

  3. WHILE LOOPS

  4. Simple While Statement Execute statements repeatedly counter = 1 while counter <= 5: print("Inside the loop") counter = counter + 1 Key word Condition : indentation

  5. Boxes A while statement has an inside while condition A: Renter the box while 'A is true

  6. Using a Counter Initialise, test, increment initialise counter = 1 while counter <= 5: print("Inside the loop") counter = counter + 1 test increment

  7. Quiz: Counter Values What is the sequence of counter values? counter = 1 while counter <= 5: print("Inside the loop") counter = counter + 1

  8. Quiz: Counter Values counter = 1 while counter <= 5: print("Inside the loop") counter = counter + 1 Counter 1 2 3 4 5 6 Print "Inside the loop" "Inside the loop" "Inside the loop" "Inside the loop" "Inside the loop" -

  9. Quiz: Counter Values How many lines printed? counter = 7 while counter <= 15: print("Inside the loop") counter = counter + 2

  10. Quiz: Counter Values counter = 7 while counter <= 15: print("Inside the loop") counter = counter + 2 Counter 7 9 11 13 15 17 Print "Inside the loop" "Inside the loop" "Inside the loop" "Inside the loop" "Inside the loop" -

  11. 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

  12. Varying the Code Repeated counter = 1 while counter < 6: print( The counter is , counter) counter = counter + 1 Counter 1 2 3 4 5 6 Print The counter is 1 The counter is 2 The counter is 3 The counter is 4 The counter is 5 -

  13. Varying the Code Repeated cntr = 1 while cntr < 6: if cntr % 2 == 0 : print( The counter is , cntr) cntr = cntr + 1 Counter 1 2 3 4 5 6 Print The counter is 2 The counter is 4 -

  14. 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)

  15. 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

  16. 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

  17. COMPARISONS

  18. Loops in Scratch Scratch has 4 loops Loop without end Loop fixed number of time Conditio n Loop while condition true Conditio n Loop until condition true

  19. 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

  20. WHILEWITH CONDITION Not all loops have counters

  21. 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

  22. 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 )

  23. TEACHING ISSUES

  24. 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

More Related Content