Do you know the capital city of a country?
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:
whileIs 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.
Questions to think about with this program to check your understanding:
Identify a Boolean variable in the program.
correct is a Boolean variable, also known as a flag.
The while condition could be written in three different ways:
while (!correct)while (correct == false)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 (!, ==, !=)?
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)
Change the program so that:
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.
// 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.");
---
}
---
}