✔ Enhanced For Loop aka For Each Loop | (Video 69)

Published: 25 September 2019
on channel: Rex Jones II
145
5

The Enhanced For Loop is also called a for each loop. It is a streamlined way to cycle a collection of elements such as an array. Regarding collections, this loop is recommended and enhanced over a traditional for loop because it eliminates:
1. the need for a loop counter
2. a start and end value
3. manually indexing the collection

► Download Transcript https://www.rexjones2.com/java-enhanc...
► Documentation via GitHub https://github.com/RexJonesII/Seleniu...

► Connect With Me via LinkedIn   / rexjones34  
► Subscribe To This Channel https://www.youtube.com/c/RexJonesII?...

► Free Selenium PDF Book: https://www.rexjones2.com/book-seleni...
► Free Java PDF Book: https://www.rexjones2.com/book-part-1...
► All Paperback & eBooks: http://tinyurl.com/Rex-Allen-Jones-Books

► Connect With Me On LinkedIn   / rexjones34  

► Transcript
Hello and Welcome, My name is Rex Jones II. In this video, we are going to look at the Enhanced For Loop also known as for each loop. The purpose of this loop is to cycle through a collection of objects.

First, we need a collection. The collection will be an array int number[]. Initialize the array to = {1, 2, 3, 4, 5}; Next, we add a variable called total and assign it to 0. This will hold the total of the array.

Now, here’s the Enhanced For Loop. We can write for then CTRL + SPACE and select for each. This is the fast way but let’s write it from scratch and walk through it step-by-step. for () The enhanced for loop accepts 3 parameters in the parenthesis: the type, iteration variable, and collection.

int is the type of data and it matches the same type as the array followed by an iteration variable. Let’s make the iteration variable num. num receives all of the elements from the array when looping through the array. Then we have a colon and name of the array: number. number is the collection that will be cycled through.

Next is the loop’s body. Print the numbers:
sysout System.out.println("Number: " + num);

We can write: total + = num; This statement will take each element 1, 2, 3, 4, 5 and add the value to total each time it loops. So, 1 gets added to 0, then 2 gets added to 1, and it will continue until we have 15 which is the total of all these numbers.

Let’s go ahead and print the total: sysout

System.out.println("--------------- \n Total: " + total);
Execute the program. 15. That’s it and Thank You for watching How To Implement An Enhanced For Loop.

#EnhancedForLoop #ForEachLoop #BeginnerSeleniumTutorials #SeleniumWebDriver


Watch video ✔ Enhanced For Loop aka For Each Loop | (Video 69) online without registration, duration hours minute second in high quality. This video was added by user Rex Jones II 25 September 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 145 once and liked it 5 people.