In this programme, we show you how to use a counter controlled iteration, known as a for loop, which is an alternative to a condition controlled repetition or while loop.
00:14
We're going to make use of an extra command that will cause the programme to pause for one second before executing the next command.
00:21
This command is not built into C by default, so we'll import that from the system threading library with the using command.
00:30
As always, we then start with our boilerplate code.
00:36
Let's write a subprogram that will count down from 10 to 0.
00:40
Static void countdown.
00:43
No data is required.
00:44
So, open and close brackets, output T before the countdown begins.
00:52
So counting can be performed with the for command, we need a variable to store the counter and this can be called anything.
01:00
It's not unusual to use a single letter here, such as C, N or I, depending on what you're counting.
01:06
But here we'll identify our variable as counter, since that's what it's going to be used for.
01:13
We want the counter to start at 10, so int counter 10.
01:19
The first part of the command ends here, so we need a semicolon.
01:23
The next part is what will cause the programme to continue iterating.
01:27
We want to keep looping when the counter is greater than zero, followed by another semicolon.
01:34
Then finally, what causes the counter to go down in each iteration.
01:39
Here we want to say counter, counter minus one.
01:43
So the shorthand for that is counter.
01:47
Remember that increments or goes up by one and minus minus means decrement or go down by one.
01:56
The code for our iteration must be enclosed in curly brackets, just like while loops console, writeline the counter, with three dots to make the output look prettier.
02:07
Before we start the next iteration, we want to pause for one second.
02:11
This is where we make use of the sleep command in the threading library.
02:15
Thread sleep will require data, so open bracket followed by the number of milliseconds to wait, that's 1,000 for one second.
02:25
Close bracket.
02:28
That's all the code we want to loop, so close the curly bracket.
02:32
So what happens here is the variable counter is initialised to 10, and then it's subtracted by 1 at the end of these commands and it goes back up and checks if it's still greater than zero.
02:44
If it is, it loops again, and if not, it stops after the countdown from 10 to 1 we'll output the final number 0.
02:54
Of course, we could have done this in the loop if we'd made the condition counter greater than minus one.
03:00
We didn't do that because we don't want a one second wait after zero is reached.
03:06
Instead, we want to immediately output all engines running, followed by liftoff.
03:11
We have liftoff on Artemis 1.
03:15
Now wait or sleep for a second before outputting Towerclear.
03:21
That's it for the subprogram.
03:23
Let's now call that subprogram in the main programme section with countdown, which requires no data.
03:30
So open and close brackets give the programme a run and we can see the effects of the for loop and counter variable counting down.
03:40
Due to the way I've captured the screen, we don't see the weight in the video, but you should see it when you run the programme.
03:46
You might be thinking, I can achieve all this with a while command and a counter variable.
03:51
Why do I need a for loop?
03:53
Indeed you can, but generally, if we know how many times we want to iterate, we use a for instead of a while.
04:01
In future programmes, we'll also see how useful a for can be to iterate through arrays and lists without, indexing, making the loop more useful in those cases.