Enhance loop in java Program

class EnhancedForLoop 
{
public static void main(String[] args) 
{
int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
for (int t: primes) 
{ 
System.out.println(t);
}
}
}
//For String
class EnhancedForLoop
{
public static void main(String[] args) 
{
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};
for (String sample: languages) 
{ 
System.out.println(sample);
}
}
}

Part-1: For Each Loop with an Integer Array primes[]

Code Explanation

1. Declaring the class

class EnhancedForLoop

It declares a class called EnhancedForLoop. The main method is the starting point for the program to run.

2. Main Approach:

This is the start of the Java program. All the code that is written within this method gets executed when the program runs.

3. Declaration and Initialization of Array:

int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

An array of integers of the type int named primes[] and initializing the same, has 10 integer values which are all prime numbers.

4. Enhanced For Loop:

 for (int t: primes)

 {

     System.out.println(t);

 }

  • That is the enhanced for loop sometimes called the for-each loop. It iterates over each of the elements of the primes array.
  • int t: primes is read as “for every element t in the array primes”.
  • Inside the loop, System.out.println(t); prints the value of t, which is each element of the array, one at a time.

5. Execution of the Loop:

The loop will run 10 times (once for each element in the primes array), printing out each prime number in the array.

Output for Integer Array (primes[]):

2

3

5

7

11

13

17

19

23

29

Every prime number from the array is printed in a new line.

Part 2: Enhanced for loop with language array ( languages[])

Code Explanation:

1. Class Definition (Again):

This section is called again by the class name EnhancedForLoop and should be put in a new class file or appended to the previous one. For clarity, it is assumed that the second part is in a new EnhancedForLoop class.

2. Main Method:

public static void main(String[] args)

It is the second program’s entry point.

3. Declaration and Initialization of Array:

String languages[] = { “C”, “C++”, “Java”, “Python”, “Ruby”} ;

An array languages[] is declared and initialized with 5 strings that represent programming languages.

4. Enhanced For Loop:

for (String sample: languages)

System.out.println(sample);

 }

  • ‘For’ loop enhanced iteration goes through every element in the languages array.
  •    Sample for string: languages means “for each element sample in the languages array”.
  • The above loop will print out the value of sample that is every string in the languages array one at a time.

5. Execution of Loop

The loop will run 5 times as there are 5 elements in the languages array and will print out each programming language in the array.

Output for String Array (languages[]):

C

C++

Java

Python

Ruby

Every programming language from the array is printed on a new line.

Combined Output of Both Programs:

1. For the integer array (primes[]):

2

3

5

7

11

13

17

19

 23

 29

2. For the string array (languages[]):

 C

 C++

 Java

 Python

Ruby

Summary:

  • Enhanced For Loop: The enhanced for loop is a more concise and readable way to iterate over arrays (or other iterable objects) in Java. It eliminates the need for a loop counter (index) and directly gives each element of the array.
  • Integer Array (primes[]) Example: The program prints out each prime number in the array, one per line.
  • Array of Strings (languages[]) Example: The following code will print out each programming language on a different line.
  • The simple but very efficient approach when working with an array or a collection is when you don’t know the index of the elements that you’re going to iterate through.

Leave a Comment

Your email address will not be published. Required fields are marked *