Do While Loop
The Do…While loop is a repetition structure where the statements inside the loop are executed at least once. Only then after being executed once, the Boolean expression is evaluated. If the Boolean expression is true, the body of the loop is executed again and the Boolean expression is re-evaluated once again. Note that this is different from the while loop, where the Boolean expression is at the top. Being at the top in a while loop, it is evaluated first and there might be a circumstance where the Boolean expression is false, right from the beginning. In this case the while loop will never happen. In a Do…While loop, the statements will always happen at least once.
Note that not all programming languages have a Do…While loop structure.
The Do…While loop (in most computer programming languages) takes the generic form of:
DO
statement(s)
…
counter = counter + 1
WHILE (boolean expression)
In this example program once again the user is asked to enter a positive integer and the program will count how many times it goes through the loop until it reaches that number. This time a Do…While loop is used.
Top-Down Design for Do…While loop
Flowchart for Do…While loop
Pseudocode for Do…While loop
GET positive_integer
DO
SHOW counter
counter = counter + 1
WHILE (counter < positive_integer)
Code for Do…While loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | // Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program uses a do ... while loop
#include <stdio.h>
int main() {
// this function uses a do ... while loop
int counter = 0;
int positiveInteger;
// input
printf("Enter how many times to repeat: ");
scanf("%d", &positiveInteger);
printf("\n");
// process & output
do {
printf("%d time(s) through the loop.\n", counter);
counter = counter + 1;
} while (counter < positiveInteger);
printf("\nDone.\n");
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | // Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program uses a do ... while loop
#include <iostream>
int main() {
// this function uses a do ... while loop
int counter = 0;
int positiveInteger;
// input
std::cout << "Enter how many times to repeat: ";
std::cin >> positiveInteger;
std::cout << std::endl;
// process & output
do {
std::cout << counter << " time(s) through the loop." << std::endl;
counter = counter + 1;
} while (counter < positiveInteger);
std::cout << "\nDone." << std::endl;
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | /* Created by: Mr. Coxall
* Created on: Sep 2020
* This program uses a do ... while loop
*/
using System;
/*
* The Program class
*/
class Program {
static void Main() {
// this function uses a do ... while loop
int counter = 0;
int positiveInteger;
// input
Console.Write("Enter how many times to repeat: ");
positiveInteger = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
// process & output
do {
Console.WriteLine(counter + " time(s) through the loop.");
counter = counter + 1;
} while (counter < positiveInteger);
Console.WriteLine("\nDone.");
}
}
|
| // No Do ... While loop in Go
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | /*
* This program uses a do ... while loop
*
* @author Mr Coxall
* @version 1.0
* @since 2020-09-01
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// this function uses a do ... while loop
// create Scanner object for user input
Scanner scanner = new Scanner(System.in);
int counter = 0;
// input
System.out.print("Enter how many times to repeat: ");
String positiveIntegerStr = scanner.nextLine();
System.out.println();
// process & output
int positiveInteger = Integer.parseInt(positiveIntegerStr);
do {
System.out.println("%d time(s) through the loop.".formatted(counter));
counter = counter + 1;
} while (counter < positiveInteger);
// close the Scanner object
scanner.close();
System.out.println("\nDone.");
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | /* Created by: Mr. Coxall
* Created on: Sep 2020
* This program uses a do ... while loop
*/
const prompt = require("prompt-sync")()
let counter = 0
// input
const positiveIntegerStr = prompt("Enter how many times to repeat: ")
console.log("")
// process & output
const positiveInteger = parseInt(positiveIntegerStr)
do {
console.log(`${counter} time(s) through the loop.`)
counter = counter + 1
} while (counter < positiveInteger)
console.log("\nDone.")
|
| # No Do ... While loop in Python
|
Example Output