Repetiton Structures - Getting A Grip On The Loop
Lon Hosford

Repetition structures are called loops in programming. Their use is to repeat processing the same lines of codes a number of times. This is contrasted to inserting those lines of code over and over for each iteration you need repeating. Not only would you end up with a long program, but single changes would require repeating the change in many places.

Loops have a basic logic to their construction and how they work.

Loops depend upon logic expressions

All loops in Java work with a logic expression. Each time the lines of code inside a loop repeat, the loop's logic expression is checked for truth. If true is detected, the lines of code are repeated. If false is detected, the lines are no longer repeated and the program continues on any lines that follow the loop.

For the logic expression to change from true to false, the data in the logic expression needs to change. This occurs inside the repetition structure or as a part of the loop's syntax.

The endless loop

If the logic expression controlling the loop never becomes false, then the loop continues endlessly. We call that an endless loop, infinite loop or indefinite loop. An endless loop may require terminating the program from the operating system or rebooting the computer.

Three types of loop structures in Java

Java has three loop structures.

The loop structures are for, do while and while.

The while Loops

The while and the do while share same loop components with the simple difference of whether or not the loop could be skipped. Said another way the difference is if the loop must process at least one time.

This is all based on when the logic expression is tested in the loop. For a do while, the logic expression is tested at the end of the loop for the first pass. For a while loop, the logic expression is tested at the start of the loop for the first pass.

When the logic expression is tested at the beginning of the loop is called a pre-test loop. This is the while loop and the while appears at the beginning of the loop.

The do while is the post test version of while loop and the while appears at the end of the loop.

Pre-test loops have the potential of being skipped whereas posttest loops are always traversed one time. In other words pre-test loops can occur zero to and infinite number of times and posttest loops occur one to an infinite number of times.

The for Loops

The for loop is a specialized loop designed for counting. It is only a pre-test loop.

The all powerful while pre-test loop

The pre-test while loop can do all the work of the other two loop structures. The other loop structures are a matter of convenience and specialization to simplify coding but do expand the learning curve.:-)

The curly brace block symbolsA loop is a block and uses the famous curly braces in Java to mark the beginning and end of the block. The lines that need repeating are inside the loop block.The loop would occur endlessly unless we tell it to stop or said another way "break out of the loop". A seemingly idle computer screen is running loopsYou might think that when the computer screen appears idle that the programs are paused. Actually as you look at your seemingly idle computer screen, programs are busily running loops.

The loops continue testing for something to happen and then action is taken. For example, you click on a button requires a loop running that waits for mouse clicks and code is selected to run based on the location on the screen the mouse point is over.