Loops in Java

 

Loops in Java

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

While loop:-

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. 

The while loop can be thought of as a repeating if statement.

Syntax :

while (boolean condition)

{

   loop statements...

}

Flowchart:-



Example:-

This program will try to print “Hello World” 5 times.

// Java program to illustrate while loop.
  
class Demo
{
     public static void main(String args[])
        {
        
           int i = 1;
                     while (i < 6)
                                    {
                        System.out.println("Hello World");
                         i++;
                  }
        }
}

Output:
Hello World
Hello World
Hello World
Hello World
Hello World

Example 2:-

// Java program to illustrate while loop

class Whileloopdemo

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

For loop:-

for loop provides a concise way of writing the loop structure.
Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.

Syntax:-

for (initialization condition; testing condition;increment/decrement)

    {

        statement(s);

   }

Flowchart:-





Example 1: This program will try to print “Hello World” 5 times.

// Java program to illustrate for loop
class Demo
{
        public static void main(String args[])
        {
        
            for (int i = 1; i <= 5; i++)
                {
                    System.out.println("Heelo World");
               }
        }
}


Output:
Hello World
Hello World
Hello World
Hello World
Hello World
Example 2

// Java program to illustrate for loop.

class Demo

{

        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

Do while:-

do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.

Syntax:

do

{

    statements..

}

while (condition);

Flowchart:-

Example 1: 

This program will try to print “Hello World” 5 times.

class Demo
{
        public static void main(String args[])
        {
  
          int i = 1
            do
             {
  
               System.out.println("Hello World"); 
                i++; 
              }  while (i < 6); 
          }

}
Output:

Hello World
Hello World
Hello World
Hello World
Hello World



Comments

Popular posts from this blog

Java Package

Life cycle of a Thread and creating thread class

Inheritance in Java