© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
R. VystavělC# Programming for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-7147-6_23

23. Unknown Number of Repetitions

Radek Vystavěl1  
(1)
Ondřejov, Czech Republic
 

In all the loops you have solved so far, you knew the number of iterations. Sometimes, you do not know it when writing a program, simply because the user is supposed to enter it. However, in all the cases, when a loop started, it had already been determined how many times it would iterate.

Sometimes, the number of repetitions is not known at the moment a loop starts executing. Frequently, you will be concerned with the question of whether a loop should go on or terminate.

Entering a Password

The first task concerns logging in. You do not know in advance how many attempts the user will need.

Task

You will repeatedly ask the user to enter a password until the user enters the correct one (see Figure 23-1). The correct password will be friend.
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig1_HTML.jpg
Figure 23-1

Repeatedly asking a question

Solution

Here’s the code:
static void Main(string[] args)
{
    string correctPassword = "friend";
    bool ok; // the variable must be declared outside of the loop!
    do
    {
        // Input
        Console.Write("Enter password: ");
        string enteredPassword = Console.ReadLine();
        // Evaluating
        ok = enteredPassword == correctPassword;
    } while (!ok); // loop repeats when the condition holds
    Console.WriteLine("Come inside, please...");
    // Waiting for Enter
    Console.ReadLine();
}

do-while Construction

To write the loop, you use the do-while construct .

The computer enters the loop after the word do, executes its statements, and asks “Once more?”. If the condition after the while word holds, the computer returns to the beginning of the loop, in other words, after the do word. And so on.

The loop terminates at the moment when its condition after the while word is evaluated as unfulfilled (false).

This Case

In this case, the program evaluates the entered password after each input. The evaluation result is then stored in a bool-typed variable called ok.

You want the loop to go on if the entered password is incorrect. That is why you use a negation operator (an exclamation mark) in the while condition.

Variable Outside of the Loop

C# requires that all variables used in the loop condition be declared outside of the loop. When you declare them inside, they are not visible when specifying the condition.

Tip

Visual Studio can help you with your do-while loops . Just enter do and press the Tab key twice.

Waiting for Descend

Imagine the computer watches some quantity that grows most of the time, and the task is to detect the (possibly rare) moment when it lessens (descends).

You would usually encounter such a problem when digging through a large amount of data stored in a file or in a database. However, you will resolve this on data entered by the user.

Task

You will make a program that repeatedly asks the user for input (see Figure 23-2). Whenever the user enters a number less than the previous one, the program will notify the user (and terminate).
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig2_HTML.jpg
Figure 23-2

Terminating when the number gets smaller

Solution

The core of the solution is to remember the previous value, not just the value currently entered.

Here’s the code:
static void Main(string[] args)
{
    // Preparations
    int previous = int.MinValue;
    bool ok;
    // Repeating until descend
    do
    {
        // Input
        Console.Write("Enter a value (number): ");
        string input = Console.ReadLine();
        int value = Convert.ToInt32(input);
        // Evaluating
        ok = value >= previous; // ok = still not descending
        // Storing for the next round of the loop
        previous = value;
    } while (ok);
    // Message to the user
    Console.WriteLine("Descend detected...");
    // Waiting for Enter
    Console.ReadLine();
}

Discussion

The first value is somewhat special because it has no predecessor. Its absence can be circumvented by simulating it using some very small number. C# offers you int.MinValue, which is the minimum value that can be stored in the int type, which is minus two billion approximately.

Every Week Until the End of Year

Let’s proceed to the next exercise, which has to do with dates.

Task

The task is to display dates until the year’s end starting with today and proceeding in one-week steps (see Figure 23-3).
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig3_HTML.jpg
Figure 23-3

Stepping through the year, one week at a time

Solution

Here’s the code:
static void Main(string[] args)
{
    // Today
    DateTime today = DateTime.Today;
    int thisYear = today.Year;
    // Repeating
    DateTime date = today;
    do
    {
        // Output
        Console.WriteLine(date.ToLongDateString());
        // Preparing next output (a week later)
        date = date.AddDays(7);
    } while (date.Year == thisYear);
    // Waiting for Enter
    Console.ReadLine();
}

As Long As the Number Six Is Being Thrown

Random numbers can provide you with other nice examples of the uncertain termination of a loop.

Task

You will throw a die and keep throwing it as long as there is a six (see Figures 23-4 and 23-5).
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig4_HTML.jpg
Figure 23-4

Rolling a die once (no six, no repetitions)

../images/458464_2_En_23_Chapter/458464_2_En_23_Fig5_HTML.jpg
Figure 23-5

Rolling a die as long as you get a six

You may know some board game where this principle is used.

Solution

Here’s the code:
static void Main(string[] args)
{
    // Random number generator
    Random randomNumbers = new Random();
    // Throwing as long as we have six
    int thrown;
    do
    {
        thrown = randomNumbers.Next(1, 6 + 1);
        Console.WriteLine(thrown);
    } while (thrown == 6);
    // Waiting for Enter
    Console.ReadLine();
}

Until the Second Six

This task is about the unknown number of repetitions with random values.

Task

You will write a program that throws a die until the six is thrown for the second time (see Figure 23-6).
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig6_HTML.jpg
Figure 23-6

Waiting until a six appears twice

Solution

You simply count the number of times a six die is thrown.

Here’s the code:
static void Main(string[] args)
{
    // Random number generator
    Random randomNumbers = new Random();
    // Throwing until the second six is thrown
    int howManySixes = 0;
    do
    {
        // Actual throwing
        int thrown = randomNumbers.Next(1, 6 + 1);
        Console.WriteLine(thrown);
        // Counting sixes
        if (thrown == 6)
        {
            howManySixes++;
        }
    } while (howManySixes < 2);
    // Waiting for Enter
    Console.ReadLine();
}

Until Two Sixes in a Row

Do you know why there are so many examples of throwing dice? I liked to play board games when I was a kid, can you tell?

Task

In this program, you will be throwing a die until a six is thrown twice in a row (see Figure 23-7).
../images/458464_2_En_23_Chapter/458464_2_En_23_Fig7_HTML.jpg
Figure 23-7

Two sixes in a row

Solution

Besides the currently thrown number, you need to track the previous one as well. This is similar to the program in the “Waiting for Descend” section.

If both the current and previous numbers are sixes, the program terminates.

Again, the first value is specific in not having a predecessor. That is why the previous variable starts with zero, which is a value that can never appear on a die.

Here’s the code:
static void Main(string[] args)
{
    // Random number generator
    Random randomNumbers = new Random();
    // Preparations
    int previous = 0;
    bool ending;
    // Throwing until two sixes in a row
    do
    {
        // Actually throwing
        int thrown = randomNumbers.Next(1, 6 + 1);
        Console.WriteLine(thrown);
        // Evaluating
        ending = thrown == 6 && previous == 6;
        // Preparing for next round of the loop
        previous = thrown;
    } while (!ending);
    // Waiting for Enter
    Console.ReadLine();
}

Summary

In this chapter, you studied loops with the number of repetitions not known at the time the loops start. In C#, this kind of loop can be suitably written using the do-while construct. Its function is first to execute the statements of its body and then to ask, “Once more?” You evaluate the condition, and if it holds, you execute another round of the loop.

You also saw that to use some variable in a do-while loop condition, the variable must be declared outside of the loop.

A frequent mistake when using the do-while loop is the wrong formulation of its condition. You must be careful and write it in such a way that if you want to continue looping, the condition should evaluate to true.

In a couple of tasks of this chapter, you needed some value from the previous round of a loop. For this purpose, you used a special variable where you stored the value. Of course, the first round of the loop required special treatment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.137.164