In most programming languages, there are two commonly used loops: for
and while
loops. This goes the same for Java, which is widely used as an (object-oriented programming) OOP language. In this post, you will learn which loop is suitable under which situation, and how to construct your loops based on a problem.
Which Loop to Pick?
The way for
and while
loops are constructed in the Java language allows them to be used interchangeably and in most situations that require a loop. However, we should be more selective in picking the right loop to do the job right.
The For
Loop
A for
loop is more suitable when we have knowledge of how many times we need to loop. For example, to print each character in a String
in each line, we need to use a for
loop. In this scenario, we know how many times we ought to repeat an action; the action being printing a character. We will be repeating this action as many times as there are number of characters in the String
, spaces included!
The sample output:
The for
loop to produce the result above:
String myString = "I love programming"; for(int i = 0; i < myString.length(); i++) { System.out.println("Letter at index " + i + ": " + myString.charAt(i)); }
The variable i
is the index of each character in myString
. It starts from 0 (int i = 0
), and will increment by 1 (i++
) at the end of the loop. It will end right before it reaches the length of myString
(i < myString.length()
). This means that i
will range from values 0 to 17 to run the System.out.println
statement inside the for
loop. We will exit the loop once i
reaches 18, without entering the loop again.
Essentially, we are doing this:
String myString = "I love programming"; System.out.println(myString.charAt(0)); System.out.println(myString.charAt(1)); ... ... System.out.println(myString.charAt(17));
The While Loop
In Java, there are two kinds of while
loops: the usual while
loop, and a do-while
loop. They are more or less the same thing, just that for the former one, the condition is being tested first before entering the loop, while for the latter case, we execute the codes in the loop first before testing the condition to see if we want to continue looping.
For now, let's focus on the former type of while
loop: test for the condition first, before entering the loop. Using the same example above, we may construct our codes using a while
loop:
String myString = "I love programming"; int i = 0; while(i < myString.length()) { System.out.println("Letter at index " + i + ": " + myString.charAt(i)); i++; }
* I have noticed many of my students tend to miss out Line 5 (i++
) in the while
loop codes when counting. Do be careful about this. Also why a for
loop is more desirable for situations like this (when you know how many times you need to iterate).
Like I've mentioned earlier in this post, a for
loop is usually preferable when you know how many times you need to loop. A while
loop can be used when you do not know when the loop will end. For example, when detecting an error input from users, or repeating code until the user enters 'q' to quit.
Suppose we print the text "Hello there!" until the user enters "Y" when the prompt to quit the program appears. In this scenario, we do not know how many times the while
loop will iterate because there can be any number of times the program will loop until the user enters "Y" at the prompt.
The sample output:
The while loop that produces the above result:
String quit = "N"; while(!quit.equalsIgnoreCase("Y")) { System.out.println("Hello there!"); System.out.print("Do you wish to quit? "); quit = new Scanner(System.in).nextLine(); } System.out.println("Bye!");For my RP students, the equivalent code without
Scanner
String quit = "N"; while(!quit.equalsIgnoreCase("Y")) { System.out.println("Hello there!"); quit = Helper.readString("Do you wish to quit? "); } System.out.println("Bye!");
Summing it Up
To conclude this post, we can see in the examples, a couple of situations when using a for or while loop is preferable over the other. Look out for future posts for tougher examples and solutions!
Happy coding! 🙂