Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Do while loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Demonstrating do while loops== {{Disputed section|The example is both wrong and fails to teach a lesson|date=November 2020}} These example programs calculate the [[factorial]] of 5 using their respective languages' syntax for a do-while loop. ===Ada=== {{Further|Ada (programming language)}} <!-- Ada demonstrates a rudimentary "loop"/"end loop" syntax. --> <syntaxhighlight lang="ada"> 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; </syntaxhighlight> ===BASIC=== {{Further|BASIC}} 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: <syntaxhighlight lang="vbnet"> 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 </syntaxhighlight> ===C, C++, D=== {{Further|C (programming language)|C++|D (programming language)}} <syntaxhighlight lang="c++"> int counter = 5; int factorial = 1; do { factorial *= counter--; // Multiply, then decrement. } while (counter > 0); std::println("factorial of 5 is {}", factorial); </syntaxhighlight> 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.<ref>{{cite web |title=C multi-line macro: do/while(0) vs scope block |url=https://stackoverflow.com/a/1067238 |website=Stack Overflow}}</ref> ===Fortran=== {{Further|Fortran}} With legacy ''Fortran 77'' there is no DO-WHILE construct but the same effect can be achieved with GOTO: <!-- Fixed-form source example demonstrating FORTRAN 77 implementation of a simulated do-while loop. --> <syntaxhighlight lang="fortran"> INTEGER CNT,FACT CNT=5 FACT=1 1 CONTINUE FACT=FACT*CNT CNT=CNT-1 IF (CNT.GT.0) GOTO 1 PRINT*,FACT END </syntaxhighlight> '''Fortran 90''' and later supports a DO-While construct: <syntaxhighlight lang="fortran"> program FactorialProg integer :: counter = 5 integer :: factorial = 1 factorial = factorial * counter counter = counter - 1 do while (counter /= 0) factorial = factorial * counter counter = counter - 1 end do print *, factorial end program FactorialProg </syntaxhighlight> ===Java=== {{Further|Java (programming language)}} <syntaxhighlight lang="java"> int counter = 5; int factorial = 1; do { factorial *= counter--; // Multiply, then decrement. } while (counter > 0); System.out.println("The factorial of 5 is " + factorial); </syntaxhighlight> ===Pascal=== {{Further|Pascal (programming language)}} [[Pascal (programming language)|Pascal]] uses repeat/until syntax instead of do while. <syntaxhighlight lang="pascal"> factorial := 1; counter := 5; repeat factorial := factorial * counter; counter := counter - 1; // In Object Pascal one may use dec (counter); until counter = 0; </syntaxhighlight> ===PL/I=== {{Further|PL/I}} 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. <syntaxhighlight lang="rexx"> declare counter fixed initial(5); declare factorial fixed initial(1); do until(counter <= 0); factorial = factorial * counter; counter = counter - 1; end; put(factorial); </syntaxhighlight> === Python === 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.<syntaxhighlight lang="python3"> factorial = 1 counter = 5 while True: factorial *= counter counter -= 1 if counter < 1: break print(factorial) </syntaxhighlight> ===Racket=== {{Further|Racket (programming language)}} In Racket, as in other [[Scheme (programming language)|Scheme]] implementations, a "named-let" is a popular way to implement loops: <syntaxhighlight lang="racket"> #lang racket (define counter 5) (define factorial 1) (let loop () (set! factorial (* factorial counter)) (set! counter (sub1 counter)) (when (> counter 0) (loop))) (displayln factorial) </syntaxhighlight> 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. Racket and Scheme also provide a proper do loop. <syntaxhighlight lang="scheme"> (define (factorial n) (do ((counter n (- counter 1)) (result 1 (* result counter))) ((= counter 0) result) ; Stop condition and return value. ; The body of the do-loop is empty. )) </syntaxhighlight> ===Smalltalk=== {{Further|Smalltalk}} <syntaxhighlight lang="smalltalk"> | counter factorial | counter := 5. factorial := 1. [counter > 0] whileTrue: [factorial := factorial * counter. counter := counter - 1]. Transcript show: factorial printString </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)