1. Pre-test
2. Post-test
At the start of the course we talked about repetition structure, this is called a loop. Loops are used when we need a computer to repeatedly process one or more program instructions.
The loop will run until a it is explicitly told to stop. This works like a selection structure, it must have a true and false path. This is the condition.
A repetition structure can be either a pre- test loop or a post-test loop. In a pretest loop, the loop condition is evaluated before the instructions within the loop are processed. In a post-test loop, the evaluation occurs after the instructions within the loop are processed.
Repetition structures use counters to count how many times the loop has been through to help it work out when the exit condition is met.
Counters are numeric variables and are typically assigned a beginning value of either 0 or 1, depending on the value required by the application. Assigning a beginning value to a variable is referred to as initialising. The initialisation task is performed before the loop is processed because it needs to be performed only once.
Counters must also be updated. Updating refers to the process of either adding a number to (called incrementing) or subtracting a number from (called decrementing) the counter’s value.
While Loops - A PRE-TEST LOOP
A while loop has a set of instructions that are carried out only while a condition is true.
We will do a really simple one in playgrounds to make sure we know what is happening.
A repetition structure can be either a pre- test loop or a post-test loop. In a pretest loop, the loop condition is evaluated before the instructions within the loop are processed. In a post-test loop, the evaluation occurs after the instructions within the loop are processed.
Playground
App
Repeat Loops
A repeat loop has a set of instructions that are carried before the condition is checked. This means that a post-test loop instructions will always happen at least once.
The post-test loop is not used as much as a pre-test loop, but we need to know how they work and why they work.
Re-write the while loops app so that it now works as a for loop. For added complexity, why not using a segment so all the different loops work in the same app.
Comentarios