Java for Beginners 4 – Working with Java Loop Statements

Java recognizes three types of loops, namely ‘for,’ ‘while’ and ‘do-while’ loops that all accomplish the similar goals of iterating the block of code until a certain condition is met. The following article explores the differences between each of the loop variants and demonstrates the situations when choosing one type of loop over the other can be a benefit. There are many examples of using all three types of loops, but I’ve selected the general form and the infinite loop to illustrate the primary differences.

Loop Statements (for, while, do-while)

The Java programming encourages the straightforwardness by requiring only three forms of control to implement an algorithm, namely the sequence, selection, and repetition.

“Repetition is implemented in one of three ways: while statement, do-while statement and for statement.” (Deitel and Deitel, 2014).

Count to 10

Let’s compare the general form of these three statements in a Java console application that prints the numbers from 1 to 10.

The for statement also referred to as the ‘for loop,’ provides Java developers with a way to iterate over a series of values. The arrangement of the for statement in Java has the initialization expression that starts the loop; the termination expression that assesses when to stop the condition and the increment expression which executes with each loop’s iteration.

This is illustrated in Figure 1 which also indicates the process of using for statement.

Figure 1 – ‘for’ general statement form

 

The while statement can accomplish the same results by repeatedly running the entire block for as long as the specified condition expression is evaluated to be the Boolean true, albeit it takes only the single condition parameter.

Figure 2 shows the general form of the while statement and the script to print the numbers from 1 to 10 using while.

Figure 2 – The while statement general form

 

The do-while statement also referred to as the ‘do loop,’ provides an alternative to a while statement with a single difference of evaluating the expression at the bottom of the loop instead of the top. It means that “the condition that stops the loop isn’t tested until after the statements in the loop has executed” (Lowe, 2014). The do-while expression is explained in Figure 3, where I show how to print the numbers from 1 to 10 using do-while statement.

Figure 3 – The while statement general form

 

Arrays

All three statements can also be used with arrays and collections (Figure 4), but the for statement is the most readable for iteration through arrays, the example shows iteration in both directions.

Figure 4 – The for statement and arrays

 

 

Infinite Loops

Now, let’s look at the some of the other differences by looking at creating the infinite loop in all three types of statements.

In the for statement (Figure 5) we can create the infinite loop by omitting all three expressions (initialization, termination, and increment), which shows that the expressions are entirely optional for the for statement.

Figure 5 – The for ‘infinite’ loop

 

In the while statement, the endless loop can be accomplished by passing ‘true’ as the condition itself (Figure 6).

It is because while can only execute when the condition equals to the Boolean true.

Figure 6 – The while statement infinite loop

 

The do-while requires the Boolean true to execute, which allows us to create an endless iteration because the repetition condition can be set to be always Boolean true (Figure 7).

Figure 7 – The do-while statement infinite loop

 

Comparison

In the general form and arrays, we can see, proven by the examples above, that all three types of Java loops can be configured to accomplish the similar results.

However, in my view, the for loops are far better suited for cases when we are aware of the exact number of iterations ahead of time. In my opinion, the explicitly stated terminate condition should allow for loops to be pre-compiled and theoretically faster than while loop. Additionally, thanks to the increment expression we can also effortlessly either increment or decrement a value, which isn’t as straightforward in the while or do-while loop.

In the infinite loop comparison, the disadvantage of for loop is often “its lack of easy interpretation” Laureate (2018), as we can see, it is more readable to use the while loop for such purposes. The while loops should be more appropriate for situations like an infinite loop, where the total number of iterations is unknown or when we want to run the loop based on the result of an external condition.

As far as using a do-while loop, I would use them when I need to guarantee the execution of the block of code at least once, because in the while loop if the initial condition is false, the statement will never execute.

 

 

References

Lowe, D. (2014). Java All-in-one for Dummies. John Wiley & Sons.

Deitel, P. & Deitel, H. (2014). Java How to Program – Early Objects (10th ed.). Pearson/Deitel Publication.

Laureate. (2018). Object-Oriented Programming in Java. Available at: https://elearning.uol.ohecampus.com/bbcswebdav/institution/UKL1/201840JAN/MS_CKIT/CKIT_510/readings/UKL1_CKIT_510_Week03_Lecture_Notes.pdf [Accessed 26 Jan. 2018].

Comments

comments