Basic Java – 11 || for and for-each in Java. (Loops Part-1)

           Sometimes we need to perform a set of instruction(s) repeatedly. We have to use for and for-each statements in Java Programming. Instructions within a loop statement repeat itself until a predefined condition.

For Loop

for Loop
for Loop

           Along with the key feature of, repeating a set of instruction(s) using a for loop, it also used to traverse through an array having a fixed size. A for loop statement consist three parameters as mentioned below:

for-statement

  1. For is a Java reserved keyword to declare for loop.
  2. { } curly parentheses, used to define a block of for loop.
  3. Initialization is the first parameter to declare and set a starting value/point from where loop should start using an integer variable.
  4. Initialization would be executed only once.
  5. A condition is the second parameter to validate end value/point until a loop will execute.
  6. increment/decrement is the third parameter to step up or step down in the value.
  7. Instructions are the set of code inside the blocks which needs to be executed iteratively till a condition returns True.
  8. When the condition returns False Java compiler gets out of the for block.
  9. Initialization parameter can also be declared before and outside the for loop.
  10. increment/decrement parameter can also be declared inside the block along with the instructions.
Related Links:

OOPs Concept:

Java Question And Answer:

Code Example-1:

public class ForLoop {

	public static void main(String[] args) 
	{
		// 1st Parameter: Initialization of currentVar=1 would be execute only once.
		// 2nd Parameter: validates value of currentVar is less than equal to 5.
		// 3rd Parameter: increases the value of currentVar by 1.
		for(int currentVar=1;currentVar<=5;currentVar++) 
                { 
                   System.out.println(currentVar); 
                } 
                
                System.out.println("\nExecution of loop in reverse order."); 
                // 1st Parameter: Initialization of currentVar=10 would be execute only once. 
                // 2nd Parameter: validates value of currentVar is greater than to 0. 
                // 3rd Parameter: decreases the value of currentVar by 2. 
                for(int currentVar=10;currentVar>0;currentVar=currentVar-2)
		{
		     System.out.println(currentVar);
		}
	}
}

Output:
1
2
3
4
5

Execution of loop in reverse order.
10
8
6
4

Code Example-2:

public class ForLoop_2
{
	public static void main(String[] args) 
	{
		int currentVar=5;     // 1st Parameter: Initialize currentVar.
		for(;currentVar<=60;) // 2nd Parameter: checks the condition.
		{
			System.out.println(currentVar);
			currentVar+=10; // 3rd Parameter: Increment/decrement currentVar
		}
	}
}

Output:
5
15
25
35
45
55

For-each Loop

for-each Loop
for-each Loop

            A for-each loop is similar to for loop. In addition to that, it can be used to traverse through an array or list whose size is not fixed or unknown at runtime. A for-each loop statement consists two parameters as mentioned below:

for-each Statement

  1. The same keyword for is used to declare for-each loop also only the parameters get changed.
  2. { } curly parentheses, used to define a block of for-each loop.
  3. Initialization is the first parameter to declare and set a starting value/point.
  4. Variable declared in above step would be initialized with the first element of the array/list.
  5. The data type of variable should be same as the type of array or list.
  6. List or array type variable is the second parameter.
  7. Each time it validates that whether array/list contains next element or not.
  8. If array/list contains next element it returns True otherwise it returns False.
  9. Instructions are the set of code inside the blocks which needs to be executed iteratively till a condition returns True.
  10. The number of iterations would be the same as the size of array/list.

Code Example-1:

public class Foreach {

	public static void main(String[] args) 
	{
		// Size of array is unknown or can be changed at runtime.
		int[] arrayVar={10,20,30,40,50};
		
		// 1st Parameter: Value of i would be initialized each time with the next element of the array/list.
		// 2nd Parameter: array or list variable.
		for (int currentVar : arrayVar) 
		{
			// Set of instructions to be repeat based on the array size.
			System.out.println(currentVar);
		}
	}
}
Output:
10
20
30
40
50

Code Example-2:

public class ForEach_2 {

	public static void main(String[] args) 
	{
		String[] strVar={"Google","Youtube","Yahoo","Facebook"};
		
		for (String currentElement : strVar) 
		{
			System.out.println(currentElement);
		}
	}
}

Output:
Google
Youtube
Yahoo
Facebook

1099total visits,1visits today

Leave a Reply

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