Fundamental Loops

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

typedescription
counting loop

Uses a loop control variable to count upwards or downwards (usually by an integer increment.)

sentinel-controlled loop

Loop keeps going until a special value is encountered in the data.

result-controlled loop

Loop keeps going until a test determines that the desired result has been achieved.

Fundamental Loops

flowchart

 

Each type of loop can be built using the while along with other statements. There are three things (in each of the three types of loops) that must be done correctly:

  1. The loop must be initialized.
  2. A condition must be tested.
  3. The loop body must change something that is tested.

Overlooking one of these aspects results in a defective loop. But usually each of these aspects is found in a different place in a program. No wonder that loops often go wrong!

The flow chart to the right shows what all loops must do. It could be for any of the three types of loops. The ellipses (. . . .) shows where the loop body does additional computation.

QUESTION 2:

Would it be convenient to have all three aspects combined into one statement?