do while
Definition
The do while
statement is used to create a loop.
It is similar to the while
loop except that the test condition is evaluated at the end of the loop.
Therefore, a do while
loop will always execute at least once.
Syntax
do while
A simple do while
statement has the following syntax:
do {
block;
} while ( condition );
do {
block;
} while ( condition );
do while
loop is not supported by Python.JavaScript and Java syntax.
- The condition is an expression that is evaluated at the end of each pass through the loop, and which evaluates to either
true
orfalse
. - The loop will stop executing when the condition becomes
false
. - The block is a block of code that gets executed at least once and then each time the condition evaluates to
true
.
Examples
do while
loop.Example 1: Quiz Question
For this example we will check if a user understands a concept. We can prompt the user for an answer at least once and keep prompting until the user
enters the correct answer. The do while
loop can be used to achieve this.
- The
do while
loop will ask the user to enter an answer (a, b or c) and check if the answer is correct (b). If the answer is not correct, the program loop will repeat and the user will be asked for an answer.
However, the code does not recognise an upper case character 'B' as correct. - Scanner input = new Scanner(System.in);
- answer = input.next().charAt(0);
var answer;
do {
answer = prompt("Loops at least once: a) while, b) do while, c) for","");
} while ( answer != 'b' );
alert("Correct! The do while loops at least once.");
do while
loop condition.
Scanner input = new Scanner(System.in);
char answer;
do {
System.out.println("Loops at least once: a) while, b) do while, c) for");
System.out.println("Enter a, b or c");
answer = input.next().charAt(0);
} while (answer != 'b');
System.out.println("Correct! The do while loops at least once.");
The input object allows access to the next() method to get the next word in the input. The charAt(0) function returns the first character in that string:
do while
loop is not supported by Python.- We now ensure the program is able to deal with both upper case and lower case input. The input value is converted to lower case prior to testing and will accept both 'b' and 'B' input as the correct answer.
var answer;
do {
answer = prompt("Loops at least once: a) while, b) do while, c) for"," ");
answer = answer.toLowerCase();
} while ( answer != 'b' );
alert("Correct! The do while loops at least once.");
answer
.
Scanner input = new Scanner(System.in);
char answer;
do {
System.out.println("Loops at least once: a) while, b) do while, c) for");
System.out.println("Enter a, b or c");
answer = input.next().charAt(0);
answer = Character.toLowerCase(answer);
} while (answer != 'b');
System.out.println("Correct! The do while loops at least once.");
answer
variable as a char.
In line 7 the program returns the lower case equivalent of the character, if any, or the character itself and stores it back in answer.
do while
loop is not supported by Python.Example 2: Guess a Secret Number
In this second example we will use the do while
loop to create a simple game that prompts a user to guess a secret number between 1 and 15.
The user enters guesses until their input matches the secret number.
- We create a variable
secretNumber
and give it the integer value 10. A variableuserGuess
is used to store the guess entered by the user and variableguesses
will be used to track how many guesses the user takes.
Each time through the loop we get the next guess from the user and we update the number of guesses by one. We will repeat the loop block until the number provided by user matches the value stored insecretNumber
.
When the number is guessed we will exit the loop and let the user know how many guesses they took to find the secret number. - Java requires that the variable type is defined before use.
- The nextInt() method gets the integer number entered by the user.
var secretNumber = 10;
var guesses = 0;
var userGuess;
do {
userGuess = prompt("Enter a number between 1 and 15","");
userGuess = parseInt(userGuess);
guesses++;
} while (userGuess != secretNumber);
alert("Guessed in " + guesses + " attempt(s).");
parseInt()
function.
Scanner input = new Scanner(System.in);
int secretNumber = 10;
int guesses = 0;
int userGuess;
System.out.println("Guess the Number Game");
do {
System.out.println("Enter a number between 1 and 15");
userGuess = input.nextInt();
guesses++;
} while (userGuess != secretNumber);
System.out.println("Guessed in " + guesses + " attempt(s).");
do while
is not supported by Python.
- Now we will extend the program to give the a hint (higher or lower) based on the users last input.
We create an empty string variablehint
which is updated within the loop with an appropriate message only if the users guess is higher or lower than the secret number. The value in this variable is concatenated (joined) to the message displayed when the user is asked for a guess.
var secretNumber = 10;
var guesses = 0;
var hint = '';
var userGuess;
do {
userGuess = prompt("Enter a number between 1 and 15" + hint," ");
userGuess = parseInt(userGuess);
guesses++;
if (userGuess > secretNumber) {
hint = ' (HINT: less than ' + userGuess + ')';
} else if (userGuess < secretNumber) {
hint = ' (HINT: greater than ' + userGuess + ')';
}
} while (userGuess != secretNumber);
alert("Guessed in " + guesses + " attempt(s).");
Scanner input = new Scanner(System.in);
int secretNumber = 10;
int guesses = 0; s
int userGuess;
String hint = "";
System.out.println("Guess the Number Game");
do {
System.out.println("Enter a number between 1 and 15" + hint + "");
userGuess = input.nextInt();
guesses++;
if (userGuess > secretNumber) {
hint = " (HINT: less than " + userGuess + ")";
} else if (userGuess < secretNumber) {
hint = " (HINT: greater than " + userGuess + ")";
}
} while (userGuess != secretNumber);
System.out.println("Guessed in " + guesses + " attempt(s).");
switch case
is not supported by Python.