GCSESession_23

advertisement
Session Objectives #23
COULD explain when each type of loop would be used and apply to a given
programme.
SHOULD describe methods of looping used in programming
MUST understand what is meant by the programming term iteration
Apply an appropriate loop to a programme
GCSE Computing#BristolMet
Key Words
GCSE Computing#BristolMet
Count-Controlled Loop
This is mostly known as a FOR loop and is used when a start and
end point for the loop is known, i.e you know how many times you
want that section of code to repeat for.
A variable is used to count how many times it should repeat
FOR <variable> = <starting value> TO <end value>
<instructions to be executed>
NEXT
e.g
FOR i = 1 TO 5
OUTPUT i
i = i + 1
NEXT
What will this programme do?
GCSE Computing#BristolMet
Condition – Controlled Loops
When the number of loops required is not known then a condition
can be created to determine whether the code should be repeated
or not. This is called condition controlled looping and one kind
is the WHILE loop.
WHILE <condition or logical test>
<instructions to be repeated>
END WHILE
The loop is repeated while the condition is true and exits when
false.
Another kind of condition controlled loop is the REPEAT UNTIL
loop.
REPEAT
<instructions to be repeated>
UNTIL <condition>
Notice the difference, the repeat does the instructions and then
ask the question. While asks the question first.
GCSE Computing#BristolMet
Looping Tasks
TASKS:
Create a program that counts 1 to 5. What kind of loop will you
use?
Can you use any other types of loop for this program. Discuss
and attempt to create.
What is an infinite loop and how would you write one?
Adapt your multi-calculator programme so that it gives the user
the option of continuing or exiting the programme. Prepare to
explain, using keywords, how this could be achieved. Now develop
your flow chart and then code it.
GCSE Computing#BristolMet
Download