Java for loop

Java For loo and for each loop

Java For Loop

Java For Loop lets you repeat a block of code as long as some condition is true. Java comes with different flavors like for, while and do while loop. In this tutorial, we will discuss about the simple for loop, for each loop in java.

Java For loo and for each loop

Syntax for Simple loop
for(initialization; condition; iteration) {

 block of code;

In initialization block, you can initialize variables. After that, you need to provide some condition which will be checked after each iteration of for loop and when that condition will be false, loop will exit. Then, the next iteration will happen.

Simple Example to print numbers

for(int i = 1; i <= 10; i++) {
     System.out.println("Numers is : " + i);

Initialization block lets you declare zero, one or more variables of the same type inside parentheses after for keyword. If you declare more than one variable inside for loop then you need to separate them with “,”  comma as below :

for(int i=1, z=10; i <= 10; i++) {
     // Block of code;
Declaration and initialization happens first before condition and iteration in for loop. The other two parts condition and iteration will run with each iteration of for loop.

You can also provide multiple condition at the same time in for loop as below :

for(int x = 10; ((x < 20) && (y == 2)); x++);

You can also create infinite loop in java as below, which is perfectly legal but should not be used :

for( ; ; ) {
    // you code will execute endless time.

How can you exit from for loop or how can yo skip statement inside for loop

You can simple do this by following methods

break :  If you use break statement inside for loop that means execution will simple jump to the first statement after for loop.

return : If you use return inside for loop that mean execution will return immediately to the calling method.

System.exit() : If you are using System.exit() method, It will stop the execution of program forcefully. JVM shuts down.

Increment statement doesn’t mean you need to increment something always. Increment statement is independent and you can put anything inside the third increment statement inside for loop as below

consider this example of code

int x = 3;

for(int b = 1; x != 1; System.out.println("Value of x : " + x)) {
     x = x - b;

Above code is legal, you can put any expression inside increment statement of for loop. In above example, condition is based on the value of x. Each time the value of x will decrease and at sometime, it will be one and loop will exit.

For each loop
Enhanced for loop which came as part of Java version 1.6. For each loop is mainly introduced for arrays and collection framework. It simplifies the iteration over Arrays and Collections and prevents any exceptions in case of null inside arrays and collections. We will discuss about Arrays and Collections framework later but for now we need to understand the basic syntax of for each loop in Java.

Syntax of For Each loop

for(variable declaration : Array or Collection variable) {
      // Block of code.
For example

int a[3] = {1,3,4};
for(int x : a) {
    System.out.println("Value of a : " x);

Related Post

Leave a Reply

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