Loop Statements in Java

While statement

  • While loop is used for continually executing a block of statement.
  • It repeats the statement while a given condition is true.
Syntax:

while (expresison)
{
     Statements ;
}


Example: Program for while statement

Program to print the Fibonacci series using while loop

class FiboTest
{
     public static void main(String args[])
     {
          int a = 0;
          int b = 1;
          int max = 50;
          int fib = 1;
          System.out.print("Fibonacci series is: "+a);
          while ( fib <= max )
          {
               System.out.print("  "+fib);
               fib = a+b;
               a = b;
               b = fib;
          }
     }
}


Output:
Fibonacci series is: 0  1  1  2  3  5  8  13  21  34

For loop

The for loop also repeats until given condition is met.

Syntax:

for (initialization; condition; increment/decrement)
{
     statements ;
}


Example: Program for "for loop"

Write a Java program to print even numbers between the given ranges using for loop.

class EvenNumber
{
     public static void main(String args[])
     {
          int n;
          System.out.print("Even number are: ");
          for( n = 0; n <= 20; ++n)
          {
               if (n % 2 == 0)
                    System.out.print("  "+n);
          }
     }
}


Output:
Even number are:  0  2  4  6  8  10  12  14  16  18  20

do-while loop

The statement inside the do block executes at least once until the condition of while is either true or false.

Syntax:

do
{
     Statements ;
}
while (condition) ;


Example: Simple program for do-while loop

class DoWhile
{
     public static void main(String args[])
     {
          int a = 1;
          do
          {
               System.out.println("Value of a : "+a);
               ++a;
          }
          while (a<10);
     }
}


Output:
Value of a : 1
Value of a : 2
Value of a : 3
Value of a : 4
Value of a : 5
Value of a : 6
Value of a : 7
Value of a : 8
Value of a : 9

Break Statement

  • Break statement is basically used for exit from loop.
  • In case of break it goes completely out of all the nested loops.

Example: Simple program for break statement

class BreakDemo
{
     public static void main(String args[])
     {
          int arr[] = {5,10,15,20,25,30};
          int n ;
          int search = 15;
          boolean b = false;
          for(n = 0; n < arr.length; n++)
          {
               if(arr[n] == search)
               {
                    b = true;
                    break;
               }
          }
          if(b)
               System.out.println("Number found " +search+" at index of  "+n);
          else
               System.out.println("Element is not found");
     }
}


Output:
Number found 15 at index of 2

Continue Statement

  • Continue statement is used to skip the current iteration of while, for or do-while loop.
  • It causes the immediate jump to next iteration of loop.

Example: Program for Continue statement

Program for printing odd and even number is different columns.

class ContinueDemo
{
     public static void main(String []args)
     {
          for (int j = 1; j <= 10; j++)
          {
               System.out.print(j+"  ");
               if (j % 2 != 0)
                    continue;
                    System.out.println("  ");
          }
     }
}


Output:
1  2    
3  4    
5  6    
7  8    
9  10