Coding - practical 8 Flashcards
What is the general form of a while loop?
while (condition) {
statements
}
Describe what the condition statements do in a while loop.
Condition = when should the loop run Statements = what should the loop do each time
When does the condition of the while loop get checked?
The condition of the while loop always gets checked before the loop starts!
How is a do while loop different to a while loop?
Almost exactly the same BUT
- The condition is checked at the end
- Will always run at least once!
(the only difference is at what point the condition is checked)
What is the general form of a for loop?
for (initialisation; condition; increment) {
statements
}
Describe the initialisation in a for loop.
This is where we can initialise our variable and is our for-loops start point.
Describe the condition in a for loop.
Sets the condition that controls whether the loop should continue or stop.
Describe the increment in a for loop.
What do we want to do each time our loop runs, usually we change the value of our variable in some way.
What can you use to increment or decrement by 1?
++ and –
When is it more appropriate to use a do while loop?
When the outcome of the condition is controlled by the statements within the loop. Or that you need to always do something once regardless of the condition.