Algorithms; Coding Techniques
Iterative constructs are structures used in computer programming to repeat the same computer code multiple times. Loops are the most commonly used iterative constructs. They provide mechanisms for controlling the number of times the code in the loop executes before the loop ends.
A loop is an iterative construct that allows a computer to execute a section of code zero, one, or multiple times based on whether a condition is true or false. Loops can also be designed to execute a section of code a specified number of times or to respond to external events. A counter variable may be used to track the repetitions. Without loops, programmers would need to include the same section of code multiple times within the program. Duplicating code within a program creates unnecessary work for the programmer, increases the chance of errors, and makes debugging and maintenance more difficult.
Three types of loops are commonly used in programming. (Note that different languages may refer to these by slightly different names, but they behave the same.) The first type of loop is the while loop. The statements in a while loop execute repeatedly as long as a condition is true. When the condition becomes false, the loop ends and program execution continues with the next statement. There are two variations of the while loop. In the first, the condition is checked before the code in the loop is executed. The other type, a do while loop, checks whether the condition is true after the code in the loop has executed once. Unlike the standard while loop, a do while loop always executes at least once.
The second type of loop is the for loop. The for loop is used when the requisite number of iterations is a known value. The for loop states concisely the starting value, number of iterations, and the change that occurs over that period. For loops can also be nested in order to create complex repetitions with multiple variables.
The third type of loop is the event loop. The code in an event loop waits for an external event to occur, such as the user pressing a key on the keyboard. When the loop detects that an event has occurred, it then executes code based on the event it has detected. For example, when the user presses the B key on the keyboard, an event loop can detect this action and direct the program to execute code that displays the letter b on the monitor. Multiple events, or tasks, may be registered. In that case, the event loop executes code based on the order in which the tasks were received. An event loop processes tasks until it receives a command to stop.
Event loops also allow different programs to communicate and interact. For example, one program might generate an event that triggers an event loop in another program. This form of interaction between software components is commonly used in software development.
Loops help programmers accomplish many common programming tasks. They allow programs to access databases efficiently, respond to system events, interact with the user, and operate with one another. However, programmers must choose the correct type of loop with care. If the code contained within the loop must execute at least once, then the programmer should use a do while loop. If, however, the code in a loop must only be executed if the loop's condition is true, a while loop must be used. Use of the wrong loop can result in incorrect program execution.
Loops must also be written such that when the tasks a loop is designed to accomplish are complete, the loop transfers control to the next statement. Failure to do so can result in an infinite loop. To prevent infinite loops, the programmer must ensure that the condition for all while loops will eventually be set to false, that the number of iterations specified in for loops be reached, or that the loop end for another reason, such as the presence of a break or exit command. Otherwise, infinite loops can result in the program becoming unresponsive.
If there are no customers in the database, the function returns a null value. If there is at least one customer in database, control should pass to the code within the loop so the list of customer names can be displayed. However, if there are no customers in the database, the code in the loop should not execute. To ensure this, a while loop is used to check the condition at the beginning of the loop.
Note that if a do loop were used, the code in the loop would execute once even if there were no customers in the database.
Within the loop, the following line of code displays the first customer name.
The next line uses the getNextCustomerName() function to retrieve the next customer name. This function returns a null value if there are no more customers in the database.
If customer_name is not null, another name has been retrieved from the database and the loop repeats. If it is null, the loop ends and execution continues with the next line of code.
The programmer must ensure that at some point, customer_name is set to null to avoid an infinite loop error.
Loops are used to implement many of the algorithms used in computer programming. Without loops, code would need to be repeated many times within the same program, leading to reduced performance and greater difficulty in debugging and maintenance. While loops and for loops allow programmers to implement repetitive actions flexibly and efficiently. Event loops make user interaction possible. They are thus critical to the graphical user interfaces in modern operating systems and to responsive web browsers.
—Maura Valentino, MSLIS
Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.
MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.
Schneider, David I. An Introduction to Programming using Visual Basic. 10th ed., Pearson, 2016.
Scott, Michael L. Programming Language Pragmatics. 4th ed., Morgan Kaufmann Publishers, 2016.
Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.