Do Loops
- 03:43
Using Do loops to iterative procedure.
Downloads
No associated resources to download.
Transcript
The do while loop is unlike the four next loop, and it will repeat as long as the condition stated is met.
In this example, the procedure loops through all the cells in a range and assigns a value to each cell.
Let's walk through the code.
A counter variable I is declared.
Note, the counter variable is declared as an integer as it will be used to repeat the instructions.
In this example, the counter variable is assigned a value of zero Initially.
The do while loop starts with the syntax do while and then the condition is stated.
Do while I, the counter variable is less than five.
We then state the instructions to be carried out whilst this condition is met.
Here, the value of the counter variable is incremented by one.
The value of I goes from zero to zero plus one or one.
The next instruction is to identify cell.
I equal one one in the spreadsheet.
This is cell A 1 cell one.
One of our spreadsheet is the range A 1, and it is assigned a value of 10.
The word loop is required to close the loop.
When I equals one, we run through the procedure again.
The do loop will continue whilst the condition is met.
An I is less than five. Do Loops can be useful to our code, but there is a potential for a loop to continually run.
If we have written our code incorrectly to stop a continuous loop, we can press control and break.
Another version of the do loop is a do and till loop.
The setup in terms of variables required is very similar to the do while loop an a counter variable is required.
Note the difference in the syntax.
The words do and are required at the start of the loop.
The condition is stated, do until I equals five.
The counter variable is incremented by one and goes from zero to one.
The next instruction in our code is to identify cell I one where I equals one here, cell one.
One is the same as cell A one in our spreadsheet and it is assigned a value of 10.
The word loop I is required to close the loop.
When I equals one, we go through the looping procedure.
Again, I is incremented by one to equal two.
We identify cell to one that in our spreadsheet is the next cell down.
We are in cell A2 and cell A2 is assigned a value of 10.
The do loop will continue until the condition is met and I is equal to five.
The do loop can offer more flexibility than the four next loop and will execute when a variable, not necessarily a loop. Counter meets the stated condition.