Saturday, January 11, 2025
Google search engine
HomeLanguagesJavaString Array with Enhanced For Loop in Java

String Array with Enhanced For Loop in Java

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);
        }
    }
}


Output

java
kotlin
c#
c
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments