Why do while loop
The syntax is:. Here's what it does. First, the statements are executed, then the condition is tested; if it is true , then the entire loop is executed again.
The loop exits when the test is performed and gives a false result. This kind of loop is most often used when the test doesn't make any sense until the statements have been performed at least once.
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. Some languages may use a different naming convention for this type of loop. There are two commonly used test after loops in the iteration or repetition category of control structures.
They are: do while and repeat until. This module covers both. The concept of iteration is connected to possibly wanting to repeat an action. Like all control structures, we ask a question to control the execution of the loop. The term loop comes from the circular looping motion that occurs when using flowcharting. The basic form of the do while loop is as follows:.
In most programming languages the question called a test expression is a Boolean expression. The Boolean data type has two values — true and false. Within the do while control structure there are three attributes of a properly working loop. They are:. This is looping on the true. When the test expression is false, you stop the loop and go on with the next item in the program. Notice, because this is a test after loop the action will always happen at least once.
It is called a test after loop because the test comes after the action. We just launched W3Schools videos. Get certified by completing a course today! If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. Arithmetic Assignment Comparison Logical. Boolean Values Boolean Expressions. Create References Memory Address.
0コメント