What is the loop that performs its conditional check at the bottom of the loop?

In most computer programming languages a do while loop is a control flow statement that executes a block of code and then either repeats the block or exits the loop depending on a given boolean condition.

The do while construct consists of a process symbol and a condition. First the code within the block is executed. Then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false.

Do while loops check the condition after the block of code is executed. This control structure can be known as a post-test loop. This means the do-while loop is an exit-condition loop. However a while loop will test the condition before the code within the block is executed.

This means that the code is always executed first and then the expression or test condition is evaluated. This process is repeated as long as the expression evaluates to true. If the expression is false the loop terminates. A while loop sets the truth of a statement as a necessary condition for the code's execution. A do-while loop provides for the action's ongoing execution until the condition is no longer true.

It is possible and sometimes desirable for the condition to always evaluate to be true. This creates an infinite loop. When an infinite loop is created intentionally there is usually another control structure that allows termination of the loop. For example a would allow termination of an infinite loop.

Some languages may use a different naming convention for this type of loop. For example, the Pascal and Lua languages have a "repeat until" loop, which continues to run until the control expression is true and then terminates. In contrast a "while" loop runs while the control expression is true and terminates once the expression becomes false.

Equivalent constructs[edit]

do {
    do_work();  
} while (condition);

is equivalent to

do_work();

while (condition) {
    do_work();
}

In this manner, the do ... while loop saves the initial "loop priming" with

while (true) {
   do_work();
   if (!condition) break;
}
8 on the line before the
while (true) {
   do_work();
   if (!condition) break;
}
9 loop.

As long as the continue statement is not used, the above is technically equivalent to the following (though these examples are not typical or modern style used in everyday computers):

while (true) {
   do_work();
   if (!condition) break;
}

or

LOOPSTART:
    do_work();
    if (condition) goto LOOPSTART;

Demonstrating do while loops[edit]

These example programs calculate the factorial of 5 using their respective languages' syntax for a do-while loop.

var counter: int = 5;
var factorial: int = 1;

do {
    factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);

trace(factorial);

with Ada.Integer_Text_IO;

procedure Factorial is
    Counter   : Integer := 5;
    Factorial : Integer := 1;
begin
    loop
        Factorial := Factorial * Counter;
        Counter   := Counter - 1;
        exit when Counter = 0;
    end loop;

    Ada.Integer_Text_IO.Put (Factorial);
end Factorial;

Early BASICs (such as GW-BASIC) used the syntax WHILE/WEND. Modern BASICs such as PowerBASIC provide both WHILE/WEND and DO/LOOP structures, with syntax such as DO WHILE/LOOP, DO UNTIL/LOOP, DO/LOOP WHILE, DO/LOOP UNTIL, and DO/LOOP (without outer testing, but with a conditional EXIT LOOP somewhere inside the loop). Typical BASIC source code:

Dim factorial As Integer
Dim counter As Integer

factorial = 1
counter = 5

Do 
    factorial = factorial * counter
    counter = counter - 1
Loop While counter > 0

Print factorial

int counter = 5;
int factorial = 1;

do {
    factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);

System.Console.WriteLine(factorial);

int counter = 5;
int factorial = 1;

do {
    factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);

printf("factorial of 5 is %d\n", factorial);

Do-while(0) statements are also commonly used in C macros as a way to wrap multiple statements into a regular (as opposed to compound) statement. It makes a semicolon needed after the macro, providing a more function-like appearance for simple parsers and programmers as well as avoiding the scoping problem with

LOOPSTART:
    do_work();
    if (condition) goto LOOPSTART;
0. It is recommended in CERT C Coding Standard rule PRE10-C.

int counter = 5;
int factorial = 1;

do {
    factorial *= counter--;
} while (counter > 0);

std::cout << "factorial of 5 is "<< factorial << std::endl;

do_work();

while (condition) {
    do_work();
}
0

do_work();

while (condition) {
    do_work();
}
1

With legacy FORTRAN 77 there is no DO-WHILE construct but the same effect can be achieved with GOTO:

do_work();

while (condition) {
    do_work();
}
2

Fortran 90 and later does not have a do-while construct either, but it does have a construct which uses the keywords "do while" and is thus actually the same as the for loop.

do_work();

while (condition) {
    do_work();
}
3

do_work();

while (condition) {
    do_work();
}
4

do_work();

while (condition) {
    do_work();
}
5

do_work();

while (condition) {
    do_work();
}
6

Pascal does not have a do/while; instead, it has a repeat/until. As mentioned in the introduction, one can consider a repeat/until to be equivalent to a 'do code while not expression' construct.

do_work();

while (condition) {
    do_work();
}
7

do_work();

while (condition) {
    do_work();
}
8

The PL/I DO statement subsumes the functions of the post-test loop (do until), the pre-test loop (do while), and the for loop. All functions can be included in a single statement. The example shows only the "do until" syntax.

do_work();

while (condition) {
    do_work();
}
9

Python lacks a specific do while flow control construct. However, the equivalent may be constructed out of a while loop with a break.

while (true) {
   do_work();
   if (!condition) break;
}
0

In Racket, as in other Scheme implementations, a "named-let" is a popular way to implement loops:

while (true) {
   do_work();
   if (!condition) break;
}
1

Compare this with the first example of the example for Racket. Be aware that a named let can also take arguments.

Which loop performs its conditional check at the bottom of the loop?

The do while loop checks the condition at the end of the loop. This means that the statements inside the loop body will be executed at least once even if the condition is never true.

Which loop checks the loop controlling Boolean expression at the bottom of the loop after one repetition has occurred?

while loop checks the value of the loop control variable at the bottom of the loop after one repetition has occurred. Below sample code explain do… while loop. The while loop is used for scenarios where you don't know how many times a block of code should be executed.

What kind of loop that executes its body at least once quizlet?

The for loop is this type of loop. The type of loop always executes at least once. A variable may be defined in the initialization expression of the for loop.

What is a condition controlled loop quizlet?

condition-controlled loop. A loop that uses true/false condition to control the number of times it repeats.