<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Do_while_loop</id>
	<title>Do while loop - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Do_while_loop"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;action=history"/>
	<updated>2026-09-11T03:02:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;diff=5282873&amp;oldid=prev</id>
		<title>imported&gt;Casablanca Rock: Added tags to the page using Page Curation (r from merge)</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;diff=5282873&amp;oldid=prev"/>
		<updated>2025-12-22T14:52:36Z</updated>

		<summary type="html">&lt;p&gt;Added tags to the page using &lt;a href=&quot;https://en.wikipedia.org/wiki/Page_Curation&quot; class=&quot;extiw&quot; title=&quot;wikipedia:Page Curation&quot;&gt;Page Curation&lt;/a&gt; (r from merge)&lt;/p&gt;
&lt;a href=&quot;http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;amp;diff=5282873&amp;amp;oldid=518089&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>imported&gt;Casablanca Rock</name></author>
	</entry>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;diff=518089&amp;oldid=prev</id>
		<title>imported&gt;ClueBot NG: Reverting possible vandalism by 115.243.123.202 to version by 129.97.124.133. Report False Positive? Thanks, ClueBot NG. (4396416) (Bot)</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Do_while_loop&amp;diff=518089&amp;oldid=prev"/>
		<updated>2025-05-26T02:49:43Z</updated>

		<summary type="html">&lt;p&gt;Reverting possible vandalism by &lt;a href=&quot;/wiki143/index.php?title=Special:Contributions/115.243.123.202&quot; title=&quot;Special:Contributions/115.243.123.202&quot;&gt;115.243.123.202&lt;/a&gt; to version by 129.97.124.133. &lt;a href=&quot;/wiki143/index.php?title=WP:CBFP&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;WP:CBFP (page does not exist)&quot;&gt;Report False Positive?&lt;/a&gt; Thanks, &lt;a href=&quot;/wiki143/index.php?title=WP:CBNG&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;WP:CBNG (page does not exist)&quot;&gt;ClueBot NG&lt;/a&gt;. (4396416) (Bot)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Programming control flow statement}}&lt;br /&gt;
[[File:Do-while-loop-diagram.svg|thumb|upright|Do While loop flow diagram]]&lt;br /&gt;
{{Loop constructs}}&amp;lt;!-- DO NOT remove. Discuss navigation concept at [[Talk:Do while loop#Helpbox experiment]] --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In many [[computer programming]] [[Programming language|languages]], a &amp;#039;&amp;#039;&amp;#039;do while loop&amp;#039;&amp;#039;&amp;#039; is a [[control flow]] [[Statement (computer science)|statement]] that executes a block of code and then either repeats the block or exits the loop depending on a given [[Boolean data type|boolean]] condition.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;do while&amp;#039;&amp;#039; 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 [[Truth|true]] the code within the block is executed again. This repeats until the condition becomes [[False (logic)|false]].&lt;br /&gt;
&lt;br /&gt;
Do while loops check the condition after the block of code is executed. This control structure can be known as a &amp;#039;&amp;#039;&amp;#039;post-test loop&amp;#039;&amp;#039;&amp;#039;. 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.&lt;br /&gt;
&lt;br /&gt;
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&amp;#039;s execution. A do-while loop provides for the action&amp;#039;s ongoing execution until the condition is no longer true.&lt;br /&gt;
&lt;br /&gt;
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 [[Control flow#Early exit from loops|break statement]] would allow termination of an infinite loop.&lt;br /&gt;
&lt;br /&gt;
Some languages may use a different naming convention for this type of loop. For example, the [[Pascal programming language|Pascal]] and [[Lua (programming language)|Lua]] languages have a &amp;quot;&amp;#039;&amp;#039;repeat until&amp;#039;&amp;#039;&amp;quot; loop, which continues to run &amp;#039;&amp;#039;until&amp;#039;&amp;#039; the control expression is true and then terminates. In contrast a &amp;quot;while&amp;quot; loop runs &amp;#039;&amp;#039;while&amp;#039;&amp;#039; the control expression is true and terminates once the expression becomes false.&lt;br /&gt;
&lt;br /&gt;
==Equivalent constructs==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
do {&lt;br /&gt;
    do_work();&lt;br /&gt;
} while (condition);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
is equivalent to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
do_work();&lt;br /&gt;
&lt;br /&gt;
while (condition) {&lt;br /&gt;
    do_work();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this manner, the do ... while loop saves the initial &amp;quot;loop priming&amp;quot; with &amp;lt;code&amp;gt;do_work();&amp;lt;/code&amp;gt; on the line before the &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
As long as the &amp;#039;&amp;#039;continue&amp;#039;&amp;#039; 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):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
while (true) {&lt;br /&gt;
   do_work();&lt;br /&gt;
   if (!condition) break;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
LOOPSTART:&lt;br /&gt;
    do_work();&lt;br /&gt;
    if (condition) goto LOOPSTART;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrating do while loops==&lt;br /&gt;
{{Disputed section|The example is both wrong and fails to teach a lesson|date=November 2020}}&lt;br /&gt;
These example programs calculate the [[factorial]] of 5 using their respective languages&amp;#039; syntax for a do-while loop.&lt;br /&gt;
&lt;br /&gt;
===Ada===&lt;br /&gt;
{{Further|Ada (programming language)}}&lt;br /&gt;
&amp;lt;!-- Ada demonstrates a rudimentary &amp;quot;loop&amp;quot;/&amp;quot;end loop&amp;quot; syntax. --&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ada&amp;quot;&amp;gt;&lt;br /&gt;
with Ada.Integer_Text_IO;&lt;br /&gt;
&lt;br /&gt;
procedure Factorial is&lt;br /&gt;
    Counter   : Integer := 5;&lt;br /&gt;
    Factorial : Integer := 1;&lt;br /&gt;
begin&lt;br /&gt;
    loop&lt;br /&gt;
        Factorial := Factorial * Counter;&lt;br /&gt;
        Counter   := Counter - 1;&lt;br /&gt;
        exit when Counter = 0;&lt;br /&gt;
    end loop;&lt;br /&gt;
&lt;br /&gt;
    Ada.Integer_Text_IO.Put (Factorial);&lt;br /&gt;
end Factorial;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===BASIC===&lt;br /&gt;
{{Further|BASIC}}&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;vbnet&amp;quot;&amp;gt;&lt;br /&gt;
Dim factorial As Integer&lt;br /&gt;
Dim counter As Integer&lt;br /&gt;
&lt;br /&gt;
factorial = 1&lt;br /&gt;
counter = 5&lt;br /&gt;
&lt;br /&gt;
Do&lt;br /&gt;
    factorial = factorial * counter&lt;br /&gt;
    counter = counter - 1&lt;br /&gt;
Loop While counter &amp;gt; 0&lt;br /&gt;
&lt;br /&gt;
Print factorial&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===C, C++, D===&lt;br /&gt;
{{Further|C (programming language)|C++|D (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
int counter = 5;&lt;br /&gt;
int factorial = 1;&lt;br /&gt;
&lt;br /&gt;
do {&lt;br /&gt;
    factorial *= counter--; // Multiply, then decrement.&lt;br /&gt;
} while (counter &amp;gt; 0);&lt;br /&gt;
&lt;br /&gt;
std::println(&amp;quot;factorial of 5 is {}&amp;quot;, factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 {{code|if}}. It is recommended in [[CERT C Coding Standard]] rule PRE10-C.&amp;lt;ref&amp;gt;{{cite web |title=C multi-line macro: do/while(0) vs scope block |url=https://stackoverflow.com/a/1067238 |website=Stack Overflow}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Fortran===&lt;br /&gt;
{{Further|Fortran}}&lt;br /&gt;
With legacy &amp;#039;&amp;#039;Fortran 77&amp;#039;&amp;#039; there is no DO-WHILE construct but the same effect can be achieved with GOTO:&lt;br /&gt;
&amp;lt;!-- Fixed-form source example demonstrating FORTRAN 77 implementation of a simulated do-while loop. --&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;fortran&amp;quot;&amp;gt;&lt;br /&gt;
      INTEGER CNT,FACT&lt;br /&gt;
      CNT=5&lt;br /&gt;
      FACT=1&lt;br /&gt;
    1 CONTINUE&lt;br /&gt;
      FACT=FACT*CNT&lt;br /&gt;
      CNT=CNT-1&lt;br /&gt;
      IF (CNT.GT.0) GOTO 1&lt;br /&gt;
      PRINT*,FACT&lt;br /&gt;
      END&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Fortran 90&amp;#039;&amp;#039;&amp;#039; and later supports a DO-While construct:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;fortran&amp;quot;&amp;gt;&lt;br /&gt;
program FactorialProg&lt;br /&gt;
    integer :: counter = 5&lt;br /&gt;
    integer :: factorial = 1&lt;br /&gt;
&lt;br /&gt;
    factorial = factorial * counter&lt;br /&gt;
    counter = counter - 1&lt;br /&gt;
&lt;br /&gt;
    do while (counter /= 0)&lt;br /&gt;
        factorial = factorial * counter&lt;br /&gt;
        counter = counter - 1&lt;br /&gt;
     end do&lt;br /&gt;
&lt;br /&gt;
    print *, factorial&lt;br /&gt;
end program FactorialProg&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
{{Further|Java (programming language)}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
int counter = 5;&lt;br /&gt;
int factorial = 1;&lt;br /&gt;
&lt;br /&gt;
do {&lt;br /&gt;
    factorial *= counter--; // Multiply, then decrement.&lt;br /&gt;
} while (counter &amp;gt; 0);&lt;br /&gt;
&lt;br /&gt;
System.out.println(&amp;quot;The factorial of 5 is &amp;quot; + factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Pascal===&lt;br /&gt;
{{Further|Pascal (programming language)}}&lt;br /&gt;
[[Pascal (programming language)|Pascal]] uses repeat/until syntax instead of do while.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pascal&amp;quot;&amp;gt;&lt;br /&gt;
factorial := 1;&lt;br /&gt;
counter := 5;&lt;br /&gt;
repeat&lt;br /&gt;
   factorial := factorial * counter;&lt;br /&gt;
   counter := counter - 1; // In Object Pascal one may use dec (counter);&lt;br /&gt;
until counter = 0;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===PL/I===&lt;br /&gt;
{{Further|PL/I}}&lt;br /&gt;
The [[PL/I]] DO statement subsumes the functions of the post-test loop (&amp;#039;&amp;#039;do until&amp;#039;&amp;#039;), the pre-test loop (&amp;#039;&amp;#039;do while&amp;#039;&amp;#039;), and the [[for loop]]. All functions can be included in a single statement. The example shows only the &amp;quot;do until&amp;quot; syntax.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rexx&amp;quot;&amp;gt;&lt;br /&gt;
declare counter   fixed initial(5);&lt;br /&gt;
declare factorial fixed initial(1);&lt;br /&gt;
&lt;br /&gt;
do until(counter &amp;lt;= 0);&lt;br /&gt;
    factorial = factorial * counter;&lt;br /&gt;
    counter = counter - 1;&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
put(factorial);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Python ===&lt;br /&gt;
Python does not have a DO-WHILE loop, but its effect can be achieved by an infinite loop with a breaking condition at the end.&amp;lt;syntaxhighlight lang=&amp;quot;python3&amp;quot;&amp;gt;&lt;br /&gt;
factorial = 1&lt;br /&gt;
counter = 5&lt;br /&gt;
&lt;br /&gt;
while True:&lt;br /&gt;
    factorial *= counter&lt;br /&gt;
    counter -= 1&lt;br /&gt;
    if counter &amp;lt; 1:&lt;br /&gt;
        break&lt;br /&gt;
&lt;br /&gt;
print(factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Racket===&lt;br /&gt;
{{Further|Racket (programming language)}}&lt;br /&gt;
In Racket, as in other [[Scheme (programming language)|Scheme]] implementations, a &amp;quot;named-let&amp;quot; is a popular way to implement loops:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;racket&amp;quot;&amp;gt;&lt;br /&gt;
#lang racket&lt;br /&gt;
(define counter 5)&lt;br /&gt;
(define factorial 1)&lt;br /&gt;
(let loop ()&lt;br /&gt;
    (set! factorial (* factorial counter))&lt;br /&gt;
    (set! counter (sub1 counter))&lt;br /&gt;
    (when (&amp;gt; counter 0) (loop)))&lt;br /&gt;
(displayln factorial)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Compare this with the first example of the [[While loop#Racket|while loop]] example for Racket. Be aware that a named let can also take arguments.&lt;br /&gt;
&lt;br /&gt;
Racket and Scheme also provide a proper do loop.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;scheme&amp;quot;&amp;gt;&lt;br /&gt;
(define (factorial n)&lt;br /&gt;
    (do ((counter n (- counter 1))&lt;br /&gt;
        (result 1 (* result counter)))&lt;br /&gt;
    ((= counter 0) result) ; Stop condition and return value.&lt;br /&gt;
    ; The body of the do-loop is empty.&lt;br /&gt;
    ))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Smalltalk===&lt;br /&gt;
{{Further|Smalltalk}}&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;smalltalk&amp;quot;&amp;gt;&lt;br /&gt;
| counter factorial |&lt;br /&gt;
counter := 5.&lt;br /&gt;
factorial := 1.&lt;br /&gt;
&lt;br /&gt;
[counter &amp;gt; 0] whileTrue:&lt;br /&gt;
    [factorial := factorial * counter.&lt;br /&gt;
    counter := counter - 1].&lt;br /&gt;
&lt;br /&gt;
Transcript show: factorial printString&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [[Control flow]]&lt;br /&gt;
* [[For loop]]&lt;br /&gt;
* [[Foreach loop]]&lt;br /&gt;
* [[Repeat loop (disambiguation)]]&lt;br /&gt;
* [[While loop]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
{{Wikibooks|Ada Programming|Control}}&lt;br /&gt;
* [http://www.pixelstech.net/article/1390482950-do-%7B-%7D-while-%280%29-in-macros do {...} while (0) in C macros]&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Do While Loop}}&lt;br /&gt;
[[Category:Control flow]]&lt;br /&gt;
[[Category:Iteration in programming]]&lt;br /&gt;
[[Category:Programming language comparisons]]&lt;br /&gt;
&amp;lt;!-- Hidden categories below --&amp;gt;&lt;br /&gt;
[[Category:Articles with example Ada code]]&lt;br /&gt;
[[Category:Articles with example BASIC code]]&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:Articles with example D code]]&lt;br /&gt;
[[Category:Articles with example Fortran code]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Articles with example Pascal code]]&lt;br /&gt;
[[Category:Articles with example Racket code]]&lt;br /&gt;
[[Category:Articles with example Smalltalk code]]&lt;br /&gt;
&lt;br /&gt;
[[de:Schleife (Programmierung)#Do-While-Schleife]]&lt;/div&gt;</summary>
		<author><name>imported&gt;ClueBot NG</name></author>
	</entry>
</feed>