Java for & foreach loop

  1. Syntax
  2. for loop (int Values) in Java
  3. for loop (float Values) in Java
  4. foreach loop in Java
  5. foreach (enum Values) loop in Java
  6. foreach (ArrayList<Double>) loop in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Syntax


for loop

for(initialize; condition; increment) {
 
	// Statement Loop..
}

foreach loop

for(type varName : iterableObj) {
 
	// Get Iterable Object's Individual Value..
}




B. for loop (int Values) in Java


For_Ex1.java

public class For_Ex1 {
	
	void display(int n) {
		
		for(int i=1; i<=n; i++) {
			
			System.out.println("for - i : " + i);
		}
	}
}
 
class MainClass {
	
	public static void main(String[] args) {
		
		For_Ex1 obj = new For_Ex1();
		
		obj.display(5);
	}
}

Sample Output

for - i : 1
for - i : 2
for - i : 3
for - i : 4
for - i : 5




C. for loop (float Values) in Java


For_Ex5.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);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		For_Ex5 obj = new For_Ex5();
		obj.display();
	}
}

Sample Output

for - Float Value : 0.0
for - Float Value : 0.1
for - Float Value : 0.2
for - Float Value : 0.3
for - Float Value : 0.4
for - Float Value : 0.5




D. foreach loop in Java


For_Ex2.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);
			i = i * 100;	// No defect on value..
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		For_Ex2 obj = new For_Ex2();
		obj.display();
	}
}

Sample Output

foreach -  i : 10
foreach -  i : 20
foreach -  i : 30
foreach -  i : 40
foreach -  i : 50
foreach -  i : 60




E. foreach (enum Values) loop in Java


For_Ex3.java

public class For_Ex3 {
	
	enum days {Monday, Tuesday, Wednesday, 
		Thursday, Friday, Saturday, Sunday};
		
	void display() {
		
		for(days day : days.values()) {
			
			System.out.println("Enum Value : " + day);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
			
		For_Ex3 obj = new For_Ex3();
		obj.display();
	}
}

Sample Output

Enum Value : Monday
Enum Value : Tuesday
Enum Value : Wednesday
Enum Value : Thursday
Enum Value : Friday
Enum Value : Saturday
Enum Value : Sunday




F. foreach (ArrayList<Double>) loop in Java


For_Ex4.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)
			sum = sum + i;
		System.out.println("The Sum of ArrayList Value is : " + sum);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		For_Ex4 obj = new For_Ex4();
		
		obj.add();
		obj.display();
	}
}

Sample Output

The Sum of ArrayList Value is : 866.9010000000001






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com