0% found this document useful (0 votes)
22 views8 pages

Computer Programming: Romelo B. Docto Ict-Teacher

The document discusses different types of for loops in Java including for loops for integer values, float values, arrays, enumerations, and ArrayLists. It provides code examples and output for each type of for loop. The for loop syntax is initialized with a starting value, contains a condition that is checked each iteration, and includes an increment statement.

Uploaded by

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

Computer Programming: Romelo B. Docto Ict-Teacher

The document discusses different types of for loops in Java including for loops for integer values, float values, arrays, enumerations, and ArrayLists. It provides code examples and output for each type of for loop. The for loop syntax is initialized with a starting value, contains a condition that is checked each iteration, and includes an increment statement.

Uploaded by

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

COMPUTER PROGRAMMING

ROMELO B. DOCTO
ICT-TEACHER
for loop Syntax

for(initialize; condition; increment) {

// Statement Loop..
}
for loop (int Values) in Java
public class For_Ex1 {

void display(int n) {

for(int i=1; i<=n; i++) {

System.out.println("for - i : " + i);


} Output?
}
}
public static void main(String[] args) { for -i :1
for -i :2
For_Ex1 obj = new For_Ex1();
for -i :3
obj.display(5); for -i :4
} for -i :5
for loop (float Values) in Java
public class For_Ex5 {

void display() {

for(float i=(float) 0.0; i<=0.5; i = (float) (i + 0.1)) {

System.out.println("for - Float Value : " + i);


} Output?
}
}
public static void main(String args[]) { for - Float Value : 0.0
for - Float Value : 0.1
For_Ex5 obj = new For_Ex5(); for - Float Value : 0.2
obj.display(); for - Float Value : 0.3
} for - Float Value : 0.4
for - Float Value : 0.5
for each loop Syntax

for(type varName : iterableObj) {

// Get Iterable Object's Individual


Value..
}
foreach loop in Java
public class For_Ex2 {

int[] value = {10, 20, 30, 40, 50, 60};

void display() {

for(int i : value) {
System.out.println("foreach - i : " + i); Output?
i = i * 100; // No defect on value..
}
} foreach - i : 10
} foreach - i : 20
public static void main(String args[]) { foreach - i : 30
foreach - i : 40
For_Ex2 obj = new For_Ex2(); foreach - i : 50
obj.display();
foreach - i : 60
}
foreach (enum Values) loop in Java
public class For_Ex3 {
enum days {Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday};

void display() {

for(days day : days.values()) {


Output?
System.out.println("Enum Value : " + day);
}
Enum Value : Monday
}
Enum Value : Tuesday
}
Enum Value : Wednesday
public static void main(String args[]) {
Enum Value : Thursday
Enum Value : Friday
For_Ex3 obj = new For_Ex3(); Enum Value : Saturday
obj.display(); Enum Value : Sunday
}
foreach (ArrayList<Double>) loop in Java
import java.util.ArrayList;

public class For_Ex4 {

ArrayList<Double> list = new ArrayList<Double>();

double sum = 0.0;

void add() {

list.add(111.120);

list.add(102.658);

list.add(653.123);

void display() {

for(double i : list)
Output?
sum = sum + i;

System.out.println("The Sum of ArrayList Value is : " + sum);

}
The Sum of ArrayList
}

public static void main(String args[]) {


Value is :
For_Ex4 obj = new For_Ex4(); 866.9010000000001
obj.add();

obj.display();

You might also like