While Loop
The While loop is a repetition structure where the statements in the structure are repeated as long as a Boolean expression is true. The flow of logic keeps coming back up to the Boolean expression to check if it is still true. As soon as the Boolean expression is false, the flow of logic hops down to the end of the loop. Note the Boolean condition is also checked before the looping statements are executed the first time. This mean if the condition is not true the first time, the statements will never happen.
It is a common occurrence to have an accumulator or counter within a looping structure. The counter, usually, is incremented (1 is added) or decremented (1 is subtracted) each time the condition is met and the statements inside the loop are performed. When the counter reaches a certain number that is expressed inside the Boolean statement, then the Boolean expression becomes false and the loop is exited. Ensure you use proper style and DO NOT do what is very common in programming, just declare the variable as i, j or x. Always name a variable for what it is actually doing and holding. For example, if you are using a counter, name it counter or loopCounter. (Yes, I know “i” is short hand for iterator; just do not use it!)
The while loop (in most computer programming languages), takes the generic form of:
In this example program, 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.
Top-Down Design for While loop

Flowchart for While loop

Pseudocode for While loop
Code for While loop
1// Copyright (c) 2020 Mr. Coxall All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program uses a while loop
6
7#include <stdio.h>
8
9int main() {
10 // this function uses a while loop
11 int counter = 0;
12 int positiveInteger;
13
14 // input
15 printf("Enter how many times to repeat: ");
16 scanf("%d", &positiveInteger);
17 printf("\n");
18
19 // process & output
20 while (counter < positiveInteger) {
21 printf("%d time(s) through the loop.\n", counter);
22 counter = counter + 1;
23 }
24
25 printf("\nDone.\n");
26 return 0;
27}
1// Copyright (c) 2020 St. Mother Teresa HS All rights reserved.
2//
3// Created by: Mr. Coxall
4// Created on: Sep 2020
5// This program uses a while loop
6
7#include <iostream>
8
9int main() {
10 // this function uses a while loop
11 int counter = 0;
12 int positiveInteger;
13
14 // input
15 std::cout << "Enter how many times to repeat: ";
16 std::cin >> positiveInteger;
17 std::cout << std::endl;
18
19 // process & output
20 while (counter < positiveInteger) {
21 std::cout << counter << " time(s) through the loop." << std::endl;
22 counter = counter + 1;
23 }
24
25 std::cout << "\nDone." << std::endl;
26 return 0;
27}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program uses a while loop
4*/
5
6using System;
7
8/*
9 * The Program class
10*/
11class Program {
12 static void Main() {
13 // this function uses a while loop
14
15 int counter = 0;
16 int positiveInteger;
17
18 // input
19 Console.Write("Enter how many times to repeat: ");
20 positiveInteger = Convert.ToInt32(Console.ReadLine());
21 Console.WriteLine();
22
23 // process & output
24 while (counter < positiveInteger) {
25 Console.WriteLine(counter + " time(s) through the loop.");
26 counter = counter + 1;
27 }
28
29 Console.WriteLine("\nDone.");
30 }
31}
1// No While loop in Go
1/*
2 * This program uses a while loop
3 *
4 * @author Mr Coxall
5 * @version 1.0
6 * @since 2020-09-01
7 */
8
9import java.util.Scanner;
10
11public class Main {
12 public static void main(String[] args) {
13 // this function uses a while loop
14
15 // create Scanner object for user input
16 Scanner scanner = new Scanner(System.in);
17
18 int counter = 0;
19
20 // input
21 System.out.print("Enter how many times to repeat: ");
22 String positiveIntegerStr = scanner.nextLine();
23 System.out.println();
24
25 // process & output
26 int positiveInteger = Integer.parseInt(positiveIntegerStr);
27
28 while (counter < positiveInteger) {
29 System.out.println("%d time(s) through the loop.".formatted(counter));
30 counter = counter + 1;
31 }
32
33 // close the Scanner object
34 scanner.close();
35 System.out.println("\nDone.");
36 }
37}
1/* Created by: Mr. Coxall
2 * Created on: Sep 2020
3 * This program uses a while loop
4 */
5
6const prompt = require("prompt-sync")()
7
8let counter = 0
9
10// input
11const positiveIntegerStr = prompt("Enter how many times to repeat: ")
12console.log("")
13
14// process & output
15const positiveInteger = parseInt(positiveIntegerStr)
16
17while (counter < positiveInteger) {
18 console.log(`${counter} time(s) through the loop.`)
19 counter = counter + 1
20}
21
22console.log("\nDone.")
1#!/usr/bin/env python3
2"""
3Created by: Mr. Coxall
4Created on: Sep 2020
5This module uses a while loop
6"""
7
8
9def main() -> None:
10 """The main() function uses a while loop, returns None."""
11 counter = 0
12
13 # input
14 positive_integer = int(input("Enter how many times to repeat: "))
15 print("")
16
17 # process & output
18 while counter < positive_integer:
19 print(f"{counter} time(s) through the loop.")
20 counter = counter + 1
21
22 print("\nDone.")
23
24
25if __name__ == "__main__":
26 main()
Example Output
