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

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 is a Java reserved keyword to declare for loop.
- { } curly parentheses, used to define a block of for loop.
- Initialization is the first parameter to declare and set a starting value/point from where loop should start using an integer variable.
- Initialization would be executed only once.
- A condition is the second parameter to validate end value/point until a loop will execute.
- increment/decrement is the third parameter to step up or step down in the value.
- Instructions are the set of code inside the blocks which needs to be executed iteratively till a condition returns True.
- When the condition returns False Java compiler gets out of the for block.
- Initialization parameter can also be declared before and outside the for loop.
- increment/decrement parameter can also be declared inside the block along with the instructions.
Related Links:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
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

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:
- The same keyword for is used to declare for-each loop also only the parameters get changed.
- { } curly parentheses, used to define a block of for-each loop.
- Initialization is the first parameter to declare and set a starting value/point.
- Variable declared in above step would be initialized with the first element of the array/list.
- The data type of variable should be same as the type of array or list.
- List or array type variable is the second parameter.
- Each time it validates that whether array/list contains next element or not.
- If array/list contains next element it returns True otherwise it returns False.
- Instructions are the set of code inside the blocks which needs to be executed iteratively till a condition returns True.
- 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
One thought on “Basic Java – 11 || for and for-each in Java. (Loops Part-1)”