« Previous Page Next Page »
    

Introduction to Java Loop control statements.

POSTED

AUTHOR: VARUN SINGH

In Java Programming, Loops control is used to execute a set of instructions/functions repeatedly while some condition evaluates to true.

Java provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.


  • 1. for loop
  • 2. while loop
  • 3. do-while loop

  • 1. Java For Loop:

    Java for loop is used to execute a sequence of statements multiple times. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line.

    There are three types of for loops in java:

  • 1. Simple For Loop
  • 2. For-each or Enhanced For Loop

  • java for loop

    1. Simple For Loop:

    The simple for loop is same as C/C++. We can initialize variable, check condition and increment/decrement value.

    Syntax: for loop:

    for(initialization; condition; incr/decr){  
    //code to be executed  
    }
    

    Example:

    // Java program to illustrate for loop.
    
    class ForLoop
    {
        public static void main(String args[])
        {
            // for loop begins when count=1  and runs till count <=5
           
            for (int count = 1; count <= 5; count++)
                System.out.println("Count:" + count);
        }
    }
    

    Output:

    Count:1
    Count:2
    Count:3
    Count:4
    Count:5
    



    2. Java for-each Loop:

    For-Each Loop is another another array traversing technique like for loop, while loop, do-while loop used to traverse the array. for-each loop reduces the code significantly and there is no use of the index or rather the counter in the loop.

    Syntax: for-each loop:

    for(Type var:array){  
    
        //code to be executed  
    
    }
    

    Example:

    public class ForEachExample {  
    public static void main(String[] args) {  
        int arr[]={10,20,30,40,50};  
        for(int i:arr){  
            System.out.println("i = "+i);  
        }  
    }  
    }
    

    Output:

    i = 10
    i = 20
    i = 30
    i = 40
    i = 50
    



    2. Java while loop:

    A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. A while loop statement in Java programming language repeatedly executes a target statement as long as a given condition is true.


    while loop

    Syntax: while loop:

    while (boolean condition)
    {
       loop statements...
    }
    

    Example:

    // Java program to illustrate while loop
    class WhileLoopDemo
    {
        public static void main(String args[])
        {
            int x = 1;
     
            // Exit when x becomes greater than 6
            while (x <= 6)
            {
                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
    Value of x:5
    Value of x:6
    




    3. Java do-while loop:

    In java, do-while loop is similar to a while loop, except that a do-while loop is execute at least one time. do-while loop first executing the statements then checks for condition.

    A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given condition at the end of the block.

    Syntax: do-while loop:

    do
    {
        statements..
    }
    while (condition);
    

    Example:

    // Java program to illustrate do-while loop
    class dowhileDemo
    {
        public static void main(String args[])
        {
            int x = 22;
            do
            {
                // The line will be printed even  if the condition is false
               
                System.out.println("Value of x:" + x);
                x++;
            }
            while (x < 24);
        }
    }
    

    Output:

    Value of x: 22
    



    The End:

    Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section. We will be very happy to hear that.

    If you like my tutorial, please follow us on facebook, Google + and Twitter. I need your support to continue.

    « Previous Page Next Page »

    Comments: