In programming languages, loops are used to execute a set of

Instructions/functions repeatedly when some conditions become true. There are three types of loops in Java.

There are 3 types of loops present in java, they are –

a. while loop

b. for loop

c. do-while loop

Let’s discuss one by one below.

For loop

The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows:

Syntax –

for (initialization; termination;
increment) {
statement(s)
}

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it’s executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Let’s make a code, which will print “welcome to microcodes.in” 5 times using for loop.

public class simpleFor {
public static void main(String[] args) {
int a;
        for(a=0;a<5;a++){
System.out.println(“welcome to microcodes.in”);
        } 
}
}
output

while loop

The while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as:

while (expression) {

statement(s)

}

The while statement evaluates expression, which must return a Boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

Let’s make a code which will print “this is a while loop” 4 times using while loop.

public class whiLe {
public static void main(String[] args) {
inti=0;
        while (i<4) {
System.out.println(“this is a while loop”);
i++;
        }
    }
}
output

do-while loop

The Java programming language also provides a do-while statement, which can be expressed as follows: Syntax –

do {

statement(s)

} while (expression);

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.

Let’s understand this below code…

public class doWhile {
    public static void main(String[] args) {
int j=2;
        do {
System.out.println(“this is a do-while loop”);
System.out.println(“j value is “+j);
j++;
        } while (j<4);
    }
}
output

break statement

When we working on loops in java, we sometime needs to terminate that loop instantly.

In that situation, we can use ‘break’ keyword.

Let’s understand this with below code…

public class breaK {
    public static void main(String[] args) {
inti=0;
        while(i<4){
System.out.println(“this is a while loop”);
System.out.println(“i value is “+i);
i++;
            break;
            // ‘break’ keyword terminate(end) loop
        }
    }
}
output

Continue statement

The continue statement skips the current iteration of a for, while, or do-while loop. The unlabeled form skips to the end of the innermost loop’s body and evaluates the Boolean expression that controls the loop.

Let’s make a code, which will inputs total 5 numbers (odd/even) and it print the addition for only even numbers.

importjava.util.*;
public class conTinue {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
intn,sum=0;
        for(inti=0;i<5;i++){
System.out.println(“enter integer: “);
            n=sc.nextInt();
            if (n%2!=0) {
                continue;
                // it’s mean we skis the addition part for odd numbers.
            }
            else{
                sum=n+sum;
            }
        }
System.out.println(“sum is “+sum);
    }
}
output

Let’s make a code, which will accepts those strings which first character start with ‘a’ and print all strings summation of all valid inputs.

import java.util.Scanner;
importjava.lang.*;
public class contiNue2 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
inti=5;
        String name,sumStr=””;
        while (i<10) {
System.out.println(“enter string: “);
            name=sc.next();
            if (name.charAt(0)!=’a’) {
                continue;
            }
            else{
System.out.println(“Your input string: “+name);
sumStr=sumStr+” “+name;
            }
System.out.println(“summation of all string: “+sumStr);
        }
    }
}
output

You will understand concept of strings later.

Nested loops

A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem.

Syntax –

While/do-while/for loop // outer loop

               While/do-while/for loop // inner loop

Let’s understand simple concepts of inner loop and outer loop with some examples below.

Nested loop (while loop)

class nestWhile{
    public static void main(String[] args) {
inti=0,j=0;
        while (i<4) {
System.out.println(“this is outer-while loop”);
            while (j<2) {
System.out.println(“this is inner-while loop”);
j++;
            }
            j=0;
i++;
        }
    }
}
output

Nested loop (for loop)

class nestFor {
public static void main(String[] args) {
inti;
    for(i=0;i<4;i++){
System.out.println(“this is outer-for loop”);
        for(int j=0;j<2;j++){
System.out.println(“this is inner-for loop”);
        }
    }
}   
}
output

Nested loop (do-while loop)

class nestDoWhile {
public static void main(String[] args) {
inti=0,j=0;
    do {
System.out.println(“outer-do-while loop”);
        do {
j++;
System.out.println(“inner-do-while loop”);
        } while (j<2);
        j=0;
i++;
    } while (i<4);
}   
}
output