Programming in D – Solutions

The while Loop

  1. Because the initial value of number is 0, the logical expression of the while loop is false since the very beginning, and this is preventing from entering the loop body. A solution is to use an initial value that will allow the while condition to be true at the beginning:
        int number = 3;
    
  2. All of the variables in the following program are default initialized to 0. This allows entering both of the loops at least once:
    import std.stdio;
    
    void main() {
        int secretNumber;
    
        while ((secretNumber < 1) || (secretNumber > 10)) {
            write("Please enter a number between 1 and 10: ");
            readf(" %s", &secretNumber);
        }
    
        int guess;
    
        while (guess != secretNumber) {
            write("Guess the secret number: ");
            readf(" %s", &guess);
        }
    
        writeln("That is correct!");
    }