• SQL Tutorial
  • RubyonRails
  • Servlet
  • Core Java
  • PHP
  • Struts
  • JSP
  • jQuery
  • JavaScript

  • <

    Loops in Java

    Looping is a feature in programming which execute set of functions repeatedly while some condition evaluates to true.

    while loop:

    A while loop is a statement that allows code to be executed repeatedly based on true and false condition.While loop checking the condition. If its true, then the loop body statements are executed otherwise first statement following the loop is executed.While Loop also called Entry control loop


    Once the condition is true, the statements in the loop body are executed.When the condition is false, the loop terminated.


    Java While Loop Example


    
    class whileLoop
    {
        public static void main(String args[])
        {
            int x = 2;
     
            
            while (x <= 4)
            {
                System.out.println("Value of x:" + x);
     
                x++;
            }
        }
    }
    

    OUTPUT:
    Value of x:2
    Value of x:3
    Value of x:4

    Java For Loop Example


    
    class forLoopDemo
    {
        public static void main(String args[])
        {
               for (int x = 2; x <= 4; x++)
                System.out.println("Value of x:" + x);
        }
    }
    

    OUTPUT:
    Value of x:2
    Value of x:3
    Value of x:4
    

    Java Enhanced For Loop Example


    
    public class enhancedforloop
    {
        public static void main(String args[])
        {
            String array[] = {"Ron", "Harry", "Hermoine"};
     
            for (String x:array)
            {
                System.out.println(x);
            }
     
            for (int i = 0; i < array.length; i++)
            {
                System.out.println(array[i]);
            }
               }
    }
    

    OUTPUT:
    Ron
    Harry
    Hermoine
    

    Java Do While Loop

    do while: is similar to while loop only difference is that it checks for condition after executing the statements.

    Do While Example


    
    class dowhileloopDemo
    {
        public static void main(String args[])
        {
            int x = 21;
            do
            {
                     System.out.println("Value of x:" + x);
                x++;
            }
            while (x < 20);
        }
    }
    

    OUTPUT:
    Value of x: 21
    












    © copyright 2017-2021 Completedone pvt ltd.