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

Do you know the capital city of a country?

Capital city

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:

while

Is used to repeat the code inside the braces underneath the statement until the condition is no longer true. E.g. while (x == 0) means repeat the code while x is still zero.

Repeated sections of code are called iterations or loops.

Boolean variables can hold a value of true or false. They are also known as flags. They are commonly used with while statements to ensure code repeats until a situation is either true or false.

You should never use an if command to repeat program statements. For example, the program below is not acceptable because the user only has one chance to get the answer wrong.

string city = Console.ReadLine();
if (city != "London")
{
    city = Console.ReadLine();
}

This is a very common mistake that new programmers make. You must always use a while to stop code continuing because you don’t know how many times an incorrect input might be made.

This mistake is easy to spot because if you use Console.ReadLine() twice with the same statement, you’ve taken the wrong approach.

Investigate

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

Item question

Identify a Boolean variable in the program.

Reveal answer

correct is a Boolean variable, also known as a flag.

Structure question

The while condition could be written in three different ways:

  1. while (!correct)
  2. while (correct == false)
  3. while (correct != false)

Which of these statements will not work, and how would you change it so that it does work without changing the operator (!, ==, !=)?

Reveal answer

Statement c will not work. It needs to say while correct does not equal true. In other words, while correct is false. This is written as:

while (correct != true)

Make

Change the program so that:

  1. The user also has to guess correctly the capital city of Spain, which is Madrid.

Typical inputs and outputs from the program would be:

What is the capital city of England?
London
What is the capital city of France?
Paris
What is the capital city of Spain?
Madrid
The quiz is complete.
🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Capital city program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Check if the answer is correct
    static bool check_answer(string country, string city)
    {
---
        // Capital city of England
        if (country == "England" && city == "London")
        {
---
            return true;
---
        }
---
        // Capital city of France
        if (country == "France" && city == "Paris")
        {
---
            return true;
---
        }
---
        // Capital city of Spain
        if (country == "Spain" && city == "Madrid")
        {
---
            return true;
---
        }
---
        // Invalid answer
        else
        {
---
            return false;
---
        }
---
    }
---

    // Quiz
    static void capital_cities()
    {
---
        bool correct = false;
        // Ask for first capital city until correct answer given
        while (!correct)
        {
---
            Console.WriteLine("What is the capital city of England?");
            string city = Console.ReadLine();
            correct = check_answer("England", city);
---
        }
---

        correct = false;
        while (!correct)
        {
---
            Console.WriteLine("What is the capital city of France?");
            string city = Console.ReadLine();
            correct = check_answer("France", city);
---
        }
---

        correct = false;
        while (!correct)
        {
---
            Console.WriteLine("What is the capital city of Spain?");
            string city = Console.ReadLine();
            correct = check_answer("Spain", city);
---
        }
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        capital_cities();
---
        Console.WriteLine("The quiz is complete.");
---
    }
---
}