3...2...1...lift off.
Select the button below to open the C# program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
Is used to repeat an indented section of code underneath the statement a known number of times.
Note The `while` command is used when you don't know how many iterations will be required, but `for` is used when the number of iterations is known.
Although you can use a while loop instead of a for loop as shown in the example below, it is not considered good practice unless the value of counter can be changed by another command in addition to the increment statement.
int counter = 0;
while (counter < 5)
{
counter = counter + 1; // This is the increment statement.
}
The syntax for a for loop is:
for (initalisation, condition, change)
{
// Code block to be executed.
}
The initialisation statement is executed first and only once. Usually, it sets a variable before the loop starts. The condition statement defines the condition for the loop to run – if it is true, the loop will repeat the code inside it, otherwise the loop will end. The change statement changes the variable initialised in the initialisation statement at the end of every iteration of the loop.
E.g.
for (int counter = 0; counter < 5; counter++)
{
Console.WriteLine(counter);
}
Delays the next line of code executing for x number of seconds. Requires using System.Threading library to access this command.
using System.Threading
Questions to think about with this program to check your understanding:
Identify an iteration ending condition and a step decrement in the program.
for (int counter = 10; counter > 0; counter--)
The iteration ending condition is counter is no longer > 0 . I.e. it equals zero or less.
The step decrement is counter-- which reduces the value of counter by one in each iteration.
State all the parts of a for loop statement.
for (int counter = 10; counter > 0; counter--)
{
}
Change the program so that it:
T minus...
12 ...
11 ...
10 ...
9 ...
Ignition sequence start.
8 ...
7 ...
6 ...
5 ...
4 ...
3 ...
2 ...
1 ...
0 ...
All engines running.
Lift off, we have a lift off on Artemis 1.
Tower clear.
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
T minus...
12 ...
11 ...
10 ...
9 ...
Ignition sequence start.
8 ...
7 ...
6 ...
5 ...
4 ...
3 ...
2 ...
1 ...
0 ...
All engines running.
Lift off, we have a lift off on Artemis 1.
Tower clear.