Countdown to launch

Countdown to launch

3...2...1...lift off.

Try


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:

Knowledge Organiser

Command summary

The new commands used in this program and others that may be useful. Select them below to learn more:

for

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);

}

Thread.Sleep(x)

Delays the next line of code executing for x number of seconds. Requires using System.Threading library to access this command.

using System.Threading

Investigate


Questions to think about with this program to check your understanding:

Item question

Identify an iteration ending condition and a step decrement in the program.

REVEAL ANSWER

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.

Structure question

State all the parts of a for loop statement.

REVEAL ANSWER

for (int counter = 10; counter > 0; counter--)

{

}

  1. The command `for` and in brackets...
  2. A counting variable, it's data type and initial value;
  3. The ending condition;
  4. An increment or decrement.
  5. Code to execute inside the loop enclosed in curly brackets.

Make


Success Criteria

Change the program so that it:

  1. Starts the countdown at 12.
  2. After 9 it outputs, "Ignition sequence start." before continuing the countdown.

Typical inputs and outputs from the program would be:

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.

Help

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.

Evaluate


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.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Bett Awards 2024 Finalist