Do while loop
Encyclopedia
In most computer programming
languages, a do while loop, sometimes just called a do loop, is a control flow
statement
that allows code to be executed repeatedly based on a given Boolean
condition. Note though that unlike most languages, Fortran
's do loop is actually analogous to the for loop
.
The do while construct consists of a block of code and a condition. First, the code within the block is executed, and 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
. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop
, which tests the condition before the code within the block is executed.
It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop
. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.
Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "while" loop runs while the control expression is true (and terminates once the expression becomes false).
is equivalent to
which (as long as the continue statement is not used) is technically equivalent to the following (though these examples are not typical or modern style):
or
of 5 using their respective languages' syntax for a do-while loop.
Note: Bash only does integer mathematics. For real numbers, try piping output through awk or bc
.
A do while loop may be employed in macros
since they shouldn't provide the final semicolon . Thus a multi-statement macro such as
can be executed only once using a do while loop where the condition is 0:
With Fortran 90 and later, the following is also valid. Using it is better practice, since it is immediately apparent that this is a while-loop.
Compare this with the first example of the while loop example for Racket.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
languages, a do while loop, sometimes just called a do loop, is a control flow
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....
statement
Statement (programming)
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components .Many languages In computer programming...
that allows code to be executed repeatedly based on a given Boolean
Boolean datatype
In computer science, the Boolean or logical data type is a data type, having two values , intended to represent the truth values of logic and Boolean algebra...
condition. Note though that unlike most languages, Fortran
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
's do loop is actually analogous to the for loop
For loop
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement....
.
The do while construct consists of a block of code and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true
True
True may refer to:* Truth, the state of being in accord with fact or reality-Music:* True , 1996* True , 2002* True , 1983** "True"...
the code within the block is executed again. This repeats until the condition becomes false
False
False or falsehood may refer to:*False *Lie or falsehood, a type of deception in the form of an untruthful statement*Falsity or falsehood, in law, deceitfulness by one party that results in damage to another...
. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop
While loop
In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement....
, which tests the condition before the code within the block is executed.
It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop
Infinite loop
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over...
. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.
Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "while" loop runs while the control expression is true (and terminates once the expression becomes false).
Equivalent constructs
is equivalent to
which (as long as the continue statement is not used) is technically equivalent to the following (though these examples are not typical or modern style):
or
Demonstrating do while loops
These example programs calculate the factorialFactorial
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n...
of 5 using their respective languages' syntax for a do-while loop.
ActionScript 3
Ada
Bash
Note: Bash only does integer mathematics. For real numbers, try piping output through awk or bc
Bc programming language
bc, for bench calculator, is "an arbitrary precision calculator language" with syntax similar to the C programming language. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell....
.
C or C++
A do while loop may be employed in macros
C preprocessor
The C preprocessor is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion , macro definitions , and conditional inclusion ....
since they shouldn't provide the final semicolon . Thus a multi-statement macro such as
swap
where the first parameter is the typeData type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
can be executed only once using a do while loop where the condition is 0:
C#
Fortran
With Fortran 90 and later, the following is also valid. Using it is better practice, since it is immediately apparent that this is a while-loop.
Java
JavaScript
Perl
PHP
Racket
In Racket, as in other Scheme implementations, a "named-let" is a popular way to implement loops:Compare this with the first example of the while loop example for Racket.