Loops in Java

while Loopfor loopdo-while loop

In the previous block, we learned about the Control Flow in Java. If you want to know more about it, visit Control Flow. In this blog, we will learn about the loops in Java. We use loops to repeat a block of the program multiple times until the specific condition satisfies. The following are the types of loops:

  1. while Loop
  2. do-while Loop
  3. for Loop

while Loop

while loop executes when the condition in the while statement is true. If the condition of the while statement is false, then the while loop will not be executed. The general syntax of a while loop is:

Syntax

while(condition){
  statements;
  .......
}

Flowchart

...

e.g

import java.util.Scanner;
public class WhileEx {
    public static void main(String args[]) {
        int numValue;
        System.out.println("Enter the table you want to print");
        Scanner scannerValue = new Scanner(System.in);
        numValue = scannerValue.nextInt();
        int i =1;
        while(i<=10){
            System.out.println(numValue + "*" + i + "=" + numValue * i);
            i++;
        }
    }
}

Output

Enter the table you want to print
34
34*1=34
34*2=68
34*3=102
34*4=136
34*5=170
34*6=204
34*7=238
34*8=272
34*9=306
34*10=340

do-while Loop

There are times when the block should be executed at least once, then we use a do-while loop. It works the same as a while loop, but the only difference is that the block gets executed at least once, even though the condition doesn’t satisfy. The general syntax is

Syntax

do {
  statement;
  ...
} while (condition);

In the above statement, the statement will be executed first and then the condition will be tested. If the condition satisfies then it will repeat the statements and retest the condition and so on.

Flowchart

...

e.g

import java.util.Scanner;
public class DoWhileEx {
    public static void main(String args[]) {
        int numValue;
        System.out.println("Enter the table you want to print");
        Scanner scannerValue = new Scanner(System.in);
        numValue = scannerValue.nextInt();
        int i =1;
        do {
            System.out.println(numValue + "*" + i + "=" + numValue * i);
            i++;
        } while(i <= 10);
    }
}

Output

Enter the table you want to print
15
15*1=15
15*2=30
15*3=45
15*4=60
15*5=75
15*6=90
15*7=105
15*8=120
15*9=135
15*10=150

Now we will see an example where the condition fails and still the block executes once.

e.g

import java.util.Scanner;
public class DoWhileEx {
    public static void main(String args[]) {
        int numValue;
        System.out.println("Enter the table you want to print");
        Scanner scannerValue = new Scanner(System.in);
        numValue = scannerValue.nextInt();
        int i =1;
          do {
            System.out.println(numValue + "*" + i + "=" + numValue * i);
            i++;
        } while( i > 10);
    }
}

Output

Enter the table you want to print
25
25*1=25

for Loop

for loop is used when we want to execute the statements iteratively for the specific number of times. The general syntax is

Syntax

for(initialization; condition; increment/decrement)

Here the first part is the initialization of the counter variable. This is the part from where the for loop starts execution. Without initialization, we cannot start the execution of the for loop. The second part is the condition, if the condition is true then it executes the statements inside the for loop. The statements are executed until the condition becomes false. The third part is increment/decrement operation. It increments/decrements the value of the counter variable.

Flowchart

...

When we declare a variable in the first part of the loop, then its scope is till the end of the for loop. Suppose we want to use the final value of the counter, then we have to declare it outside the loop, because if it's declared inside the for loop, then we cannot use the value outside the loop.

e.g

import java.util.Scanner;
public class ForEx {
    public static void main(String args[]) {
        int numValue;
        System.out.println("Enter the table you want to print");
        Scanner scannerValue = new Scanner(System.in);
        numValue = scannerValue.nextInt();

        for(int i=1;i<=10;i++){
            System.out.println(numValue + "*" + i +"=" + numValue * i);
        }
    }
}

Output

Enter the table you want to print
75
75*1=75
75*2=150
75*3=225
75*4=300
75*5=375
75*6=450
75*7=525
75*8=600
75*9=675
75*10=750

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.