您在這裡

Understanding Iteration in General -do while

6 二月, 2015 - 16:57

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:

do  some statements or action  some statements or action  some statements or action  update the flagwhile the answer to the question is true

In every language that I know the question (called a test expression) is a Boolean expression. The Boolean data type has two values _ true and false. Let's rewrite the structure to consider this:

do  some statements or action  some statements or action  some statements or action  update the flagwhile expression is true

Within the do while control structure there are three attributes of a properly working loop. They are:

  • Action or actions
  • Update of the fag
  • Test expression

The English phrasing is, "You do the action while the expression is true". 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. It is also sometimes called a post-test loop, meaning the test is post (or Latin for after) the action and update.