Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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

Countdown to launch

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

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

for

Is used to repeat the code inside the braces underneath the statement a known number of times.

Note: while 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; // increment statement
}
for (int i = x; i < y; i += z)

A for loop has three parts between the parentheses: the initialisation, the condition and the change.

  • i = x is the initialisation statement. It is executed first and only once, and usually sets a variable before the loop starts.
  • i < y is the condition statement. It is checked before every iteration: if it is true the loop repeats the code inside the braces, otherwise the loop ends.
  • i += z is the change statement. It changes the loop variable at the end of every iteration. For a decrementing loop use i-- or i -= z.
for (int counter = 0; counter < 5; counter++)
{
    Console.WriteLine(counter); // prints 0, 1, 2, 3, 4
}
Thread.Sleep(x)

Pauses execution for x milliseconds. Requires using System.Threading; at the top of your program.

using System.Threading;
Thread.Sleep(1000); // pause 1 second

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

The iteration ending condition is counter > 0 (loop exits when counter reaches 0).

The step decrement is counter--, equivalent to subtracting 1.

Structure question

State all the parts of a for loop statement.

Reveal answer
  1. The keyword for.
  2. The initialiser (e.g. int counter = 12).
  3. The continue condition (e.g. counter > 0).
  4. The iteration step (e.g. counter--).
  5. The body inside braces { }.

Make

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.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Countdown to launch program

using System;
using System.Threading;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    static void countdown()
    {
---
        Console.WriteLine("T minus...");
        for (int counter = 12; counter > 0; counter--)
        {
---
            Console.WriteLine($"{counter} ...");
            if (counter == 9)
            {
---
                Console.WriteLine("Ignition sequence start.");
---
            }
---
            Thread.Sleep(1000);
---
        }
---
        Console.WriteLine("0 ...");
        Console.WriteLine("All engines running.");
        Console.WriteLine("Lift off, we have a lift off on Artemis 1.");
        Thread.Sleep(1000);
        Console.WriteLine("Tower clear.");
        Console.ReadLine();
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        countdown();
---
    }
---
}