In Java, there is an alternative syntax of for loop to work with Java arrays and Java collections (known as for-each loop). For example,
In Java, the for-each loop is used to iterate through each element of arrays or collections. It is also known as enhanced for loop. Here's what the for-each loop looks like in Java,
Here,
The output looks like Sum = 19. We can also verity the difference between for loop and for-each loop with the help of below code blocks.