Enhanced for loop(for-each loop) was introduced in java version 1.5 and it is also a control flow statement that iterates a part of the program multiple times. This for-loop provides another way for traversing the array or collections and hence it is mainly used for traversing arrays or collections. This loop also makes the code more readable and reduces the chance of bugs in the code.
Syntax:
for(data-type variable : array | collection) { // Code to be executed }
In this article, we will see how to get array Strings using Enhanced For Loop.
Implementation
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { String str[] = { "java" , "kotlin" , "c#" , "c" }; // Using Enhanced For Loop for (String str1 : str) { System.out.println(str1); } } } |
java kotlin c# c