Sometimes you need to repeat the same code multiple times. The best way to do that is through looping. There are three basic types of loops: for, while, and do-while. All three have the same basic parts which are arranged in different ways.
- loop variable initialization (A)
- loop variable test to see if loop is to continue (B)
- loop variable change formula, using just incrementing (C)
Video
I am including a short video to go through the code for the three types of looping
Sample Code
/* Showing the methods of looping in a program.
* Wayne Cook
* 16 February 2021
* Purpose:
* This program goes through the three main types of loops: for, while, and
* do-while. Each have their purpose, but they all have similar parts.
* Loop Variables must be initialized before it is used. (A)
* It must have a test to see when the loop is to continue/end. (B)
* It must have something to change the value that is being tested. (C)
* I will go through the three loop types and label A, B, and C
*/
public class Looping {
public static void main(String[] args) {
int counter = 0; // create a variable to use in the while & do-while loops
int END_LOOP = 10; // A common value where the loop is to end.
/* "for" loop
* The for loop always starts with "for" and contains the (A), (B), and (C) parts explained above.
* Using the same variable is used for all their sub-parts helps with consistency and appearance.
* Example: for( int i = 0; i < 4; i++) where i starts at 0, goes through 3 incrementing by 1.
*/
System.out.println("do the For Loop");
for (int i = 0; i < END_LOOP; i++) {
System.out.println(" The for count is " + i);
}
System.out.println("Please notice whether the For Loop reaches " + END_LOOP);
/* while loop
* the while loop checks the test value before the loop is run. This means that the loop
* may not run if the "continue" check fails at the start of the loop.
*/
System.out.println("do the While Loop");
counter = 0; // Variable initialization (A)
while (counter < END_LOOP) { // End the loop when END_LOOP is reached. (B)
System.out.println(" The while counter is now at: " + counter++); // Increment after use. (C)
}
System.out.println("Please notice whether the While Loop reaches " + END_LOOP);
/* Do-While Loop
* the do-while loop checks the test value after the loop is run. This means that the loop
* will run even before the "continue" check fails at the end of the loop.
*/
System.out.println("do the Do-While Loop");
counter = 0; // Variable initialization (A)
do {
System.out.println(" The do-while counter is now at: " + counter++); // Increment after use. (C)
} while (counter < END_LOOP); // End the loop when END_LOOP is reached. (B)
System.out.println("Please notice whether the Do-While Loop reaches " + END_LOOP);
/* To make the difference more obvious between while and do-while, the end test will be set to
* the beginning value for each.
*/
System.out.println("Check both loops to see if anything is executed.");
System.out.println("do the While Loop");
counter = 0; // Variable initialization (A)
while (counter < counter) { // End the loop when END_LOOP is reached. (B)
System.out.println(" The while counter is now at: " + counter++); // Increment after use. (C)
}
System.out.println("Please notice whether the While Loop prints a line");
System.out.println("do the Do-While Loop");
counter = 0; // Variable initialization (A)
do {
System.out.println(" The do-while counter is now at: " + counter++); // Increment after use. (C)
} while (counter < counter); // End the loop when END_LO"Please notice whether the While Loop prints a line");
System.out.println("Please notice whether the Do-While Loop prints a line");
}
}