A programming loop is a control structure that allows a programmer to execute the same instruction or group of instructions over and over until some condition is met. All loops have a basic structure to them, though there are many types:
- The loop entry is the point at which the program's control enters the loop and begins executing the first instructions.
- The loop test at the beginning of the loop takes over the program control and determines whether the loop can begin, repeat, or skip to the commands after the loop.
- The iteration is a single cycle made through to loop, i.e. if the computer executes the loop 12 times the loop has 12 iterations.
- The termination condition is whatever is built in to the loop to cause the looping to stop. The program control exits the loop and continues exiting whatever commands come after.
Count-Controlled Loops
An obvious type of loop is the count-controlled loop, wherein the programmer explicitly states how many times he wants the loop to execute. A count-controlled loop obviously first needs some kind of counter that while increment each time the loop is executed. This is done rather simply, as shown here in this while loop:
public static void main (String args[]){
int count = 1;
while (count <= 5){
//Commands to be executed...
count=count +1;
}
}
Note that the incrementing line can also be written as follows: count=count++;
Whatever the programmer writes in the while loop will be executed five times. If the loop test was specified like count < 5, the loop would iterate only four times. Because the count variable started out at 1, the loop needs only four more iterations to reach the terminating condition.
Event-Controlled Loops
Another type of loop is the event-controlled loop. These loops are little more picky about their terminating conditions, allowing the programmer to specify how many iterations are made based on certain events rather than on an arbitrary number. As the programmer may not know how many iterations will need to be made, event-controlled loops are very useful. Three of the most common types are:
- End-of-file Loops, which receive some file as input and terminate when the loop reaches the end of the file. where the computer tries to read another line of data but finds none, a terminating condition.
- Sentinel-controlled Loops, which specify a certain input value as 'sentinel' and terminate when that value is entered as a parameter. For example, if a data file has a list of numbers from -100 to 100 and a loop only needs to perform calculations on the negative integers, 0 would be defined as a sentinel to prevent processing the positive integers in the file input.
- Flag-controlled Loops, which use a boolean variable as a flag to determine whether a terminating condition has been reached and the loop can stop processing.
SummaryLoops come in several flavors, but they all have the same basic premise: process the same commands over and over until the computer is told to stop. How the computer is told to stop determines the different types of loops, and different statements are typically used for each type, such as using the for statement for count-controlled loops. This tutorial is a gentle precursor to using those different statements, to understand the concepts behind them.
Join the Conversation