0% found this document useful (0 votes)
12 views6 pages

It Elective Module

Uploaded by

saffronstunicorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

It Elective Module

Uploaded by

saffronstunicorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

I.

INTRODUCTION

Good Day! Welcome to the third week of our Correspondence Learning


Modality. This week, you shall be given another lesson to study and another learning
task/s to submit.

Study and Assessment Guide


DATE TOPIC ACTIVITIES OR TASKS
 Read the lesson on
Java For-each Loop or
Java For-each Loop or Enhanced For Loop
November 3 TO 6 Enhanced For Loop  Do the Performance Task
II. LEARNING CONTENT

Java for-each Loop or Enhanced for Loop provides an alternative approach to traverse
the array or collection in Java. It is mainly used to traverse the array or collection elements.
The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the
code more readable. It is known as the for-each loop because it traverses each element one
by one.

The drawback of the for-each loop is that it cannot traverse the elements in reverse
order. Here, you do not have the option to skip any element because it does not work on an
index basis. Moreover, you cannot traverse the odd or even elements only.

What are the Advantages for-each or enhance loop over for loop?

 It makes the code more readable.


 It eliminates the possibility of programming errors.

How does the for-each loop works?

1. It starts with the keyword for like a normal for-loop.


2. Instead of declaring and initializing a loop counter variable, you declare a variable
that is the same type as the base type of the array, followed by a colon, followed by
the array name.
3. You can use the loop variable you created in the loop body rather than using an
indexed array element.
4. It's commonly used to iterate over an array or a Collections class (e.g., ArrayList)

Syntax

The syntax of Java for-each loop consists of data_type with the variable followed by a colon
(:), then array or collection.

for(data_type variable : array | collection){


//body of for-each loop
}

 array - an array or a collection


 item - each item of array/collection is assigned to this variable
 dataType - the data type of the array/collection

Example #1: A program that will print array elements

Output:

3
9
5
-5

We have used the for-each loop to print each element of the numbers array one by one.
1. In the first iteration, the item will be 3.
2. In the second iteration, the item will be 9.
3. In the third iteration, the item will be 5.
4. In the fourth iteration, the item will be -5.

Example #2: A program that will print the Sum of Array Elements

Output:
Sum = 19
the execution of the for each loop looks as:
Iteration Variables
1 numbers = 3
sum = 0 + 3 = 3
2 numbers = 4
sum = 3 + 4 = 7
3 numbers = 5
sum = 7 + 5 = 12
4 numbers = -5
sum = 12 + (-5) = 7
5 numbers = 0
sum = 7 + 0 = 7
6 numbers = 12
sum = 7 + 12 = 19

As we can see, we have added each element of the numbers array to the sum variable in
each iteration of the loop.

Example #3: A program that will print all Strings

Output:
Volvo
BMW
Ford
Mazda

Example # 4: A program that will print the sum of elements

Output:
The sum of elements is: 83

Example #5: A program that will print using single characters


Output:
a
b
c
d

Generalization:

Java For-each Loop or Enhanced for Loop provides an alternative approach to


traverse the array or collection in Java. It is mainly used to traverse the array or collection
elements. The advantage of the for-each loop is that it eliminates the possibility of bugs and
makes the code more readable. It is known as the for-each loop because it traverses each
element one by one. The syntax of Java for-each loop consists of data_type with the variable
followed by a colon (:), then array or collection.
for(data_type variable : array | collection){
//body of for-each loop
}
III. LEARNING TASK
Activity: Performance Task (30 points)
(printed)Topic: ArrayList

Name: Score:
Year & Section:

Declare an array AGES of 10 elements of type int with the following values 15, 18, 16,
17, 14, 12, 13, 20, 22, 25. Display the average age of a group of people using for-each or
enhance loop.

Expected Output:

Ages of the group are : 15 18 16 17 14 12 13 20 22 25

Average age of the group = 17

You might also like