In the last two chapters, we have learnt to develop programs that used simple sequential statements or conditional statements. In the first case, each statement in the program is executed in a fixed order; whereas in the second case, depending on some condition a particular set of statements gets executed, skipped or another set of statements gets executed. But most of the time we have to execute same series of actions. The mechanism, which helps us to perform an action over and over, is the ‘loop’ or ‘iteration’.
Loops are of two types, entry controlled loop and exit controlled loop. When condition is checked before the execution of the body of the loop, it is called entry controlled loop or pre-test iteration. It is described by the following block diagram.
As the block diagram described, first condition will be checked. If the condition is true, the control will enter in the loop and body of the loop will execute. Then control will loop back and the condition will be tested again. This process will be continued until the condition becomes false. When the condition becomes false, control will terminate the loop and execute the next statement followed by the loop statement.
But when condition is checked after the execution of the body of the loop, it is called exit controlled loop or post test iteration. Here, first body of the loop will execute. Then condition will be checked to determine whether the loop will execute next time or not. If the condition is true, the control will loop back. When the condition becomes false, control will terminate the loop and execute the next statement followed by the loop statement. It is described by the following block diagram.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.