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
Solution
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
Solution
The core of the solution is to remember the previous value, not just the value currently entered.
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
Solution
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 may know some board game where this principle is used.
Solution
Until the Second Six
This task is about the unknown number of repetitions with random values.
Task
Solution
You simply count the number of times a six die is thrown.
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
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.
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.