Java - For and ForEach Loops
Good day everyone! Today's topic, while we are sadly coming to the end of the conditional statements, will revolve around the For and ForEach loops for Java. Now, while they are both similar, there are some distinct differences we will explore in order to better understand them. In the first example of code, we will see a For loop, which will then be followed by the ForEach loop. I want you to pay close attention as they each have unique cases in which you would want to use them while doing development work.
For loop example
So, let's start with 1. You will notice our data structure, a String array, with an index of 5 names of String type values. We will discuss more about arrays in the upcoming weeks(DSAs). For number 2, here we see our for loop and it has some unique characteristics. We will identify these characteristics as:
2a) Our initializer, int i = 0; while this is optional, we are using this to initialize a counter of sorts for our for loop statement
2b) Our test condition, i < names.length; we are taking the initialized value and saying, as long as you are less than the length of the number of values(indexes) in our array, then this condition is true, but once it is false, then the loop will stop looping
2c) Our updation, i++; while this is optional, we are using this to increment or increase each loop count by one so we can eventually get out of the loop once the test condition is false
领英推荐
Next, for number 3, we are printing out our results of the for loop. I want you to notice the "names[i]" after the plus sign. We are asking for the printout to include the index value of the names array(each value individually). And finally, with number 4, we see the expected results.
Now, we will look at the forEach loop. As our example above, for number 1, we are starting with the same array of names and they are of String type. Going on to number 2, notice how the setup shows (String name : names). What this statement means is, that for every value name(that is a String type) in names(our collection or array to be looped over) do something. That something for us is shown to us in the third step. Finally, in number 3, we print out our results.
So, we have seen both, and you may be thinking the obvious, one has less code than the other and you would not be wrong. Yet, for efficiency's sake, the forEach loop goes through the array (iterates or traverses are common terms heard to describe looking in an array) only forward and looks for values. On the other hand, the for loop will search forward and backward to find index values, depending on how you are searching for things. The most common of the two used in development is the forEach loop due to the fact that it does not need a counter to escape the loop(infinite loops are scary lol). Well, that concludes our discussion on for loops and Java statements. I hope this helps you in your development journey working with Java.
Incoming Explore Intern @ Microsoft | Software Engineer | React | Javascript/Typescript | C++ | BSc. Computer Science
1 年Very well explained and an easily digestible read, thank you, David K.. Very useful for those getting started with Java!
Software Engineer | Frontend | Backend | Creating Value & Solutions | Sharing Insights in Software Engineering | Author Inside Tech Newsletter
1 年Thanks for this David Kea, Jr. MBA ??