Looping Statements in Java

Looping Statements in Java

In this article, I am going to discuss the Looping Statements in Java with Examples. Please read our previous article, where we discussed Decision Making Statements in Java with examples. At the end of this article, you will understand what are Looping Statements and its type with examples.

What are Looping Statements in Java?

Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true.

Types of looping statements in Java:

There are three types of looping statements in Java. They are as follows:

  1. For loop
  2. While loop
  3. Do while loop
For Loop in Java:

The Java for loop repeats the execution of a set of Java statements. A for loop executes a block of code as long as some condition is true. The syntax to use for the loop is given below.

for(initialization; condition; increment/decrement)
{
      Statement(s);
}

The initialization expression initializes the loop and is executed only once at the beginning when the loop begins. The termination condition is evaluated every loop and if it evaluates to false, the loop is terminated. And lastly, increment/decrement expression is executed after each iteration through the loop.

Flow chart of for loop:

For loop Looping Statements Flow Chart in Java

Sample Program for For Loop in Java:
class ForLoopDemo 
{ 
    public static void main(String args[]) 
    { 
 for(int num = 1; num <= 5; num++)
       {
         System.out.println(num);
       }
    } 
}

Output:
1
2
3
4
5

Enhanced for loop in Java:

Enhanced for loop provides a simpler way to iterate through the elements of a collection or array. It is inflexible and should be used only when there is a need to iterate through the elements in a sequential manner without knowing the index of the currently processed element.

Also, note that the object/variable is immutable when enhanced for loop is used, i.e it ensures that the values in the array cannot be modified, so it can be said as a read-only loop where you can’t update the values as opposed to other loops where values can be modified. The syntax to use the enhanced for loop in java is given below.

for(T element : Collection object / array)
{
     Statement(s);
}

Sample Program for Enhanced for loop in Java:
public class EnhancedforloopDemo 
{ 
    public static void main(String args[]) 
    { 
        String array[] = {"Ron", "Harry", "Hermoine"}; 
  
        //enhanced for loop 
        for (String x:array) 
        { 
            System.out.println(x); 
        } 
  
        /* for loop for same function 
        for (int i = 0; i < array.length; i++) 
        { 
            System.out.println(array[i]); 
        } 
        */
    } 
}

Output:
Ron
Harry
Hermoine

While Loop in Java:

The while statement or loop continually executes a block of statements while a particular condition is true. The while statement continues testing the expression and executing its block until the expression evaluates to false. The syntax to use while loop in java is given below.

while(condition)
{
        Statement(s);
}

Flow chart of While Loop:

While Loop Looping Statements Flow Chart in Java

Sample Program for while Loop in Java:
class whileLoopDemo 
{ 
    public static void main(String args[]) 
    { 
        int x = 1; 
  
        // Exit when x becomes greater than 4 
        while (x <= 4) 
        { 
            System.out.println("Value of x:" + x); 
  
            // Increment the value of x for 
            // next iteration 
            x++; 
        } 
    } 
}

Output:
Value of x: 1
Value of x: 2
Value of x: 3
Value of x: 4

Do-while Loop in Java:

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.

Note that the do-while statement ends with a semicolon. The condition-expression must be a boolean expression. The syntax to use the do-while loop is given below.

do{
       Statement(s);
}
while(condition);

Flow chart of the do-while loop:

Do While Loop Looping Statements Flow Chart in Java

Sample Program for do-while Loop in Java:
class dowhileloopDemo 
{ 
    public static void main(String args[]) 
    { 
        int x = 21; 
        do
        { 
            // The line will be printed even 
            // if the condition is false 
            System.out.println("Value of x:" + x); 
            x++; 
        } 
        while (x < 20); 
    } 
}

Output: Value of x: 21

In the next article, I am going to discuss Branching Statements in Java with examples. Here, in this article, I try to explain Looping Statements in Java with Examples and I hope you like this Looping Statements in Java with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about Looping Statements in Java with Examples article.

Leave a Reply

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