Loop

Learn Loop System in Java with Programming Example

In this chapter you will learn:Loop

  1. What is loop in Java?
  2. Explain For loop, do while loop and while loop
  3. Loop programming example
 What is loop in Java?

Loop is very important statement in any programming. Loop is a techniques to repeat a block of code certain times. Sometimes you need to execute a task multiple times especially working with array when you need to store or retrieve value. Loop helps you to read all element of array.

How many types of loop available in Java?

Java provides following looping constructs to work with.

  1. For Loop
  2. While Loop
  3. Do While Loop

Apart from it there are two looping control statements are available which decide whether your loop will be terminated in meanwhile the process or continue by skipping current iteration of loop.

(i) Break statement – It terminated the loop completely and forward execution to outside from loop.
(ii) Continue Statement – It skips only current iteration of loop. It doesn’t break the loop.

For Loop:

For loop is most common but most usable loop in Java. It requires 3 condition to work properly.

(i) Initialization – Starting point of loop
(ii) Termination – Loop breaks when reaches this point.
(iii) Increment/Decrement – Increment or decrement loop iteration.

Syntax:

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

Programming Example

class ForLoop
{
  public static void main(String [] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
    for(int i=0; i<7; i++)
    {      
      System.out.println(str[i]);
    }
  }
}

Output

D:\JavaProgram>javac ForLoop.java

D:\JavaProgram>java ForLoop
sunday
monday
tuesday
wednesday
thursday
friday
saturday

D:\JavaProgram> __

Another way to use For Loop in Java (Like Foreach)

class ForeachLoop
{
  public static void main(String[] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};   
    for(String s: str)
    {
      System.out.println(s);
    }
  }
}

Output

D:\JavaProgram>javac ForeachLoop.java

D:\JavaProgram>java ForeachLoop
sunday
monday
tuesday
wednesday
thursday
friday
saturday

D:\JavaProgram> __

 

While Loop

In while loop you initialize the loop variable before the loop starts. While are just keeping test whether condition is true or not and inside the while condition increment/decrement process done.

Programming Example

class WhileLoop
{
  public static void main(String [] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};    
    int i = 0;
    while(i<7)
    {
      System.out.println(str[i]);
      i++;
    }
  }
}

Output

D:\JavaProgram>javac WhileLoop.java

D:\JavaProgram>java WhileLoop
sunday
monday
tuesday
wednesday
thursday
friday
saturday

D:\JavaProgram> __

 

Do While Loop

In do while is slightly different from while loop. The while loop executes if the condition matches but in do while loop statement, the loop starts first then check condition in second iteration. It means whether or not condition is matches, the loop executes one iteration by default.

Programming Example

class DoWhileLoop
{
  public static void main(String [] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};    
    int i = 0;
    do
    {
      System.out.println(str[i]);
      i++;
    }
    while(i<7);
  }
}

Output

D:\JavaProgram>javac DoWhileLoop.java

D:\JavaProgram>java DoWhileLoop
sunday
monday
tuesday
wednesday
thursday
friday
saturday

D:\JavaProgram> __

Break Statement:

As earlier explained; that the break statement terminates the loop and sends execution control outside the loop. Let’s understand it with suitable programming example:

class BreakStatement
{
  public static void main(String[] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
    for(int i=0; i<7; i++)
    {  
      if(str[i]=="thursday")
      {
        System.out.println("Breaking the loop. sorry!!!");
        break;
      }
      System.out.println(str[i]);
    }
  }
}

Output

D:\JavaProgram>javac BreakStatement.java

D:\JavaProgram>java BreakStatement
sunday
monday
tuesday
wednesday
Breaking the loop. sorry!!!

D:\JavaProgram> __

Continue Statement:

The continue statement skips the current iteration of loop and increment loop value by 1. It doesn’t terminate the loop instead it only terminate the current iteration of loop. Let's understand it with suitable programming example:

Programming Example

class ContinueStatement
{
  public static void main(String[] args)
  {
    String [] str = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
    for(int i=0; i<7; i++)
    {  
      if(str[i]=="thursday")
      {
        System.out.println("This information is not available. Sorry!!!");
        continue;
      }
      System.out.println(str[i]);
    }
  }
}

Output

D:\JavaProgram>javac ContinueStatement.java

D:\JavaProgram>java ContinueStatement
sunday
monday
tuesday
wednesday
This information is not available. Sorry!!!
friday
saturday

D:\JavaProgram> __

Return Statement

The return statement exits execution control from the current method and send it where method was invoked. There are two types of return statement in Java. One which return a value and another one which doesn’t return any value.

Programming Example

class ReturnStatement
{
  public static void main(String[] args)
  {
    int result;
    result=add(20,30);
    System.out.println(result);    
  }
  
  public static int add(int num1, int num2)
  {
    return num1+num2;
  }
}

Output

D:\JavaProgram>javac ReturnStatement.java

D:\JavaProgram>java ReturnStatement
50

D:\JavaProgram>

Summary

In this chapter you have learned looping statement in java. You have also learned what the for loop, while loop, do while loop, return statement, break statement and continue statement in Java. In the next chapter you will learn about Decision Making in Java.

Leave a Reply

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