Do while loop - Wikipedia PDF

Title Do while loop - Wikipedia
Author Kute Face
Course Programming 1
Institution University of the People
Pages 9
File Size 361.8 KB
File Type PDF
Total Downloads 44
Total Views 146

Summary

Do while loop - Wikipedia...


Description

9/13/2020

Do while loop - Wikipedia

Do while loop In most computer programming languages, a do while loop is a control flow statement that executes a block of code at least once, and then either repeatedly executes the block, or stops executing it, depending on a given boolean condition at the end of the block. The do while construct consists of a process symbol 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, the dowhile loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. If the expression is false, the loop terminates and control transfers to the statement following the do-while loop. In other words, whereas a while loop sets the truth of a statement as a condition precedent for the code's execution, a dowhile loop provides for the action's ongoing execution subject to defeasance by the condition's falsity, which falsity (i.e., the truth of the condition's negation) is set as a condition subsequent. 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.

Do While loop flow diagram

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).

Contents Equivalent constructs Demonstrating do while loops ActionScript 3 Ada BASIC Bourne Again Shell C# C https://en.wikipedia.org/wiki/Do_while_loop

1/9

9/13/2020

Do while loop - Wikipedia

C++ CFScript D Fortran Java JavaScript Kotlin Pascal PHP PL/I Python Racket Ruby Smalltalk Swift Visual Basic .NET See also References External links

Equivalent constructs 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 do_work(); on the line before the while 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;

https://en.wikipedia.org/wiki/Do_while_loop

2/9

9/13/2020

Do while loop - Wikipedia

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

ActionScript 3 var counter: int = 5; var factorial: int = 1; do { factorial *= counter--; /* Multiply, then decrement. */ } while (counter > 0); trace(factorial);

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;

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: 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

Bourne Again Shell until [[ condition ]]; do #statements done

https://en.wikipedia.org/wiki/Do_while_loop

3/9

9/13/2020

Do while loop - Wikipedia

# does the same. while [[ condition ]]; do #statements done

C# int counter = 5; int factorial = 1; do { factorial *= counter--; /* Multiply, then decrement. */ } while (counter > 0); System.Console.WriteLine(factorial);

C 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 if. It is recommended in CERT C Coding Standard rule PRE10-C.[1]

C++ int counter = 5; int factorial = 1; do { factorial *= counter--; } while (counter > 0); std::cout 0) factorial = factorial * counter counter = counter - 1 end do print *, factorial end program FactorialProg

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); //============================================// // The below function does the same as above. // //============================================// int counter = 5; int factorial = 1; while (counter > 0){ factorial *= counter--; /* Multiply, then decrement. */ } System.out.println("The factorial of 5 is " + factorial);

JavaScript https://en.wikipedia.org/wiki/Do_while_loop

5/9

9/13/2020

Do while loop - Wikipedia

var counter = 5; // Declaring two variables, counter and factorial var factorial = 1; do { factorial *= counter--; //What will be looped } while (counter > 0); //The looping conditions console.log(factorial); //Showing the result

[3]

Kotlin var counter = 5 var factorial = 1 //These line of code is almost the same as the above JavaScript codes, the only difference is the keyword that shows the results do { factorial *= counter-} while (counter > 0) println("Factorial of 5 is $factorial")

[4]

Pascal 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. factorial := 1; counter := 5; repeat factorial := factorial * counter; counter := counter - 1; // In Object Pascal one may use dec (counter); until counter = 0;

PHP $counter = 1; $factorial = 1; $loop = 5; do { echo $factorial * $counter; $counter++; $loop--; } while ($loop > 0);

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. declare counter fixed initial(5); declare factorial fixed initial(1);

https://en.wikipedia.org/wiki/Do_while_loop

6/9

9/13/2020

Do while loop - Wikipedia

do until(counter counter 0) (loop))) (displayln factorial)

Compare this with the first example of the while loop example for Racket. Be aware that a named let can also take arguments. Racket and Scheme also provide a proper do loop. (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. ))

Ruby counter = 10 factorial = 2 begin factorial *= counter counter -= 2 end while counter > 1 puts factorial

https://en.wikipedia.org/wiki/Do_while_loop

7/9

9/13/2020

Do while loop - Wikipedia

Smalltalk | counter factorial | counter := 5. factorial := 1. [counter > 0] whileTrue: [factorial := factorial * counter. counter := counter - 1]. Transcript show: factorial printString

Swift Swift 2.x and later:[5] var counter = 5 var factorial = 1 repeat { factorial *= counter counter -= 1 } while counter > 0 print(factorial)

Swift 1.x: var counter = 5 var factorial = 1 do { factorial *= counter counter -= 1 } while counter > 0 println(factorial)

Visual Basic .NET Dim counter As Integer = 5 Dim factorial As Integer = 1 Do factorial *= counter counter -= 1 Loop While counter > 0 Console.WriteLine(factorial)

See also Control flow For loop Foreach While loop Repeat loop (disambiguation)

https://en.wikipedia.org/wiki/Do_while_loop

8/9

9/13/2020

Do while loop - Wikipedia

References 1. "C multi-line macro: do/while(0) vs scope block" (https://stackoverflow.com/a/1067238). Stack Overflow. 2. "Microsoft visual basic" (https://msdn.microsoft.com/en-us/library/ezk76t25.aspx). msdn.microsoft.com. Retrieved 21 January 2016. 3. "Mozilla Developer Network Documentation - The Do While Loop in JavaScript" (https://develope r.mozilla.org/en-US/docs/JavaScript/Reference/Statements/do...while). 4. "Kotlin Control Flow - While - Kotlin Programming Language" (https://kotlinlang.org/docs/referenc e/control-flow.html#while-loops). 5. https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID126

External links do {...} while (0) in C macros (http://www.pixelstech.net/article/1390482950-do-%7B-%7D-while-% 280%29-in-macros) Retrieved from "https://en.wikipedia.org/w/index.php?title=Do_while_loop&oldid=973864839"

This page was last edited on 19 August 2020, at 17:45 (UTC). Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.

https://en.wikipedia.org/wiki/Do_while_loop

9/9...


Similar Free PDFs