while (num != -1);
{
num = promptForDouble("Enter a number", keyboard);
System.out.println("You entered " + num);
}
is an infinite loop if num starts out as something other than -1!
If num is -1, the logical expression is false, the while ends (because of the semicolon) and then the next statement (the call to promptForDouble()) is executed once, as is the print statement.
while (num != -1)
;
num = promptForDouble("Enter a number", keyboard);
System.out.println("You entered " + num);
A continue statement causes execution to jump to the beginning of the loop and re-evaluate the logical condition.
For instance, you could write the following code
...
while (true)
{
quiz = promptForDouble("Please enter a quiz score (-1 to end): ";
if (quiz == -1)
break;
if (quiz < 0)
{
System.out.println("You must enter a positive quiz score or -1 to end.");
continue;
}
points = points + quiz;
num = num + 1;
}
average = points / num;
Is this a sentinel while loop?