With a While loop, you can execute a statement or block of statements as long as a certain condition is met.

For this kind of loop, you have to define a condition. This condition is an ordinary Boolean expression that should be True before you start the loop (otherwise, the loop will not be executed at all). You then provide a block of command statements that will be executed in the loop. Each time the loop is completed, the program will check if the condition is still met. If it is, the loop statements are executed once more. If the condition is no longer met, the loop is broken.

The crucial part of a While loop is its breaking condition. In your execution block, you have to add a line where you eventually allow the starting condition to become False. If you do not do this, the loop will never be broken and your program will probably hang. Usually, you use a counter variable that you increment or decrement to break out of the loop, but you can use any other condition of course.


While Condition Do


Starts the While loop


Boolean expression that controls the loop: As long as it is True, the loop will continue. When it becomes False, the loop is broken.


Starts the Do execution block that is executed as long as the condition is True


Any command statement or block statement that will be executed as long as the loop condition is met.

Most While loops use a block statement (Begin End section), as you have to add statements to break the loop: You must provide a possibility for the condition to become False. Otherwise, your loop will never be broken and run indefinitely.


This is a While loop that adds up all elements of source up to the value of period. Note that i is incremented inside the execution block, so that the starting condition i < period will eventually become False and end the loop.

i = 0;
While i < period Do
Result = Result + source[i];
i = i + 1;

This is of course the same example as in the section on For loops, where we needed only two lines of code for the same effect. You can see that for a given problem, different loops could be used, but that some are more fitting for a problem and thus result in fewer lines of code.