Do while loop
Encyclopedia : D : DO : DOW : Do while loop
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, 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.
For example, in the C programming language, the code fragment:
x = 0; do while (x < 3);first executes the instruction x = x + 1, which results in x = 1. It then checks the condition x < 3, which is true, so it executes the code block again. It repeats this execute and check process until the variable x has the desired value, 3.
Demonstrating do while loops
These do while loops will calculate the factorial of a number:
Dim counter As Integer : counter = 5
Dim factorial as Long : factorial = 1
Do
factorial = factorial * counter 'multiply
counter = counter - 1 'decrement
While (counter > 0)
Print factorial
unsigned int counter = 5;
unsigned long factorial = 1;
do while (counter > 0);
printf("%lu\n", factorial);
int counter = 5;
long factorial = 1;
do while (counter > 0);
System.out.println(factorial);
Dim counter As Integer = 5
Dim factorial as Integer = 1
Do
factorial = factorial * counter // multiply
counter = counter - 1 // decrement
While counter > 0
MsgBox Str( factorial )
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
Dim counter As Integer : counter = 5 Dim factorial as Long : factorial = 1 Do factorial = factorial * counter 'multiply counter = counter - 1 'decrement While (counter > 0) Print factorial
unsigned int counter = 5;
unsigned long factorial = 1;
do while (counter > 0);
printf("%lu\n", factorial);
int counter = 5;
long factorial = 1;
do while (counter > 0);
System.out.println(factorial);
Dim counter As Integer = 5
Dim factorial as Integer = 1
Do
factorial = factorial * counter // multiply
counter = counter - 1 // decrement
While counter > 0
MsgBox Str( factorial )
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
int counter = 5; long factorial = 1; do while (counter > 0); System.out.println(factorial);
Dim counter As Integer = 5 Dim factorial as Integer = 1 Do factorial = factorial * counter // multiply counter = counter - 1 // decrement While counter > 0 MsgBox Str( factorial )
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
