Erstellt von Helena Frawley
vor fast 8 Jahre
|
||
Frage | Antworten |
Iteration - Lesson 3 | - Understand and use iteration in an algorithm - Write algorithms in pseudocode involving sequence, selection and iteration - Trace through given algorithms to explain what they do, correct or complete them |
What is iteration?? | Iteration is when parts of an algorithm need repeating |
What are the two types of iteration statements in python?? | - FOR... TO ... NEXT -WHILE ... DO ... UNTIL |
Why are FOR loops useful and the most efficient type of loop?? | A FOR loop can be set for a specific amount of times, so is set up with a start value and a ending value. The loop only ends when the end value is reached. This type of loop is useful for reading values into lists. |
Example of a FOR loop: | Total <- 0(put 0 into it) FOR Counter <- 1 TO 7 MaxTemperature <- USERINPUT Add MaxTemperature to Total NEXT Counter AverageWeeksTemp <- Total/7 Output (“This week’s average is ”, AverageWeeksTemp) |
When do you use a WHILE loop?? | when the number of repetitions/iterations is not known and the actions are only repeated WHILE a given condition is TRUE. The condition must be tested at the beginning of the loop. |
Example of a WHILE loop: | emailAddress <- USERINPUT While emailAddress does not contain “@” Output “Invalid address – please re-enter” emailAddress <- USERINPUT Endwhile Output “Thank you” |
TIP: to help you understand how while loops work try laying it out as a flowchart where if the user doesn't do something it returns to the beginning. | TIP TIP TIP |
Möchten Sie mit GoConqr kostenlos Ihre eigenen Karteikarten erstellen? Mehr erfahren.