The while
Loop
- Because the initial value of
number
is 0, the logical expression of thewhile
loop isfalse
since the very beginning, and this is preventing from entering the loop body. A solution is to use an initial value that will allow thewhile
condition to betrue
at the beginning:int number = 3;
- 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!"); }