You are here

Understanding Iteration in General -while

10 February, 2015 - 10:59

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 fowcharting. The basic form of the while loop is as follows:

initialization of the flag while the answer to the question is true then do   some statements or action   some statements or action   some statements or action   update the flag 

In almost all languages 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:

initialization of the flag while the expression is true then do   some statements or action   some statements or action   some statements or action   update the flag 

Within the while control structure there are four attributes to a properly working loop. They are:

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

The initialization of the fag is not technically part of the control structure, but a necessary item to occur before the loop is started. The English phrasing is, "While the expression is true, do the following actions". 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 before loop the action might not happen. It is called a test before loop because the test comes before the action. It is also sometimes called a pre-test loop, meaning the test is pre (or Latin for before) the action and update.