Home / JAVA / Java For Loop

Java For Loop

Loops Statement:

The programming instructions are defined in a sequence and perform on the base of given instructions. It is basically used to execute instructions more than one time on the base of given condition. It reduces the programming instruction and increases the efficiency of the program. We maintain starting of the loops ending with condition and step of the loop.

For Loop Statements

For loop is a type of loop that executes the instructions after checking the condition. The looping statements are maintained in a single statement with for keyword.

 

Flowchart:-

for loops in java
image source:-https://www.javatpoint.com
Syntax:-

for ( starting of a loop; ending with the condition; steps of the loop)

{

    statement;

      }

 

Image Source:- http://java.meritcampus.com

 

Example:-

Class B

{

   public static void main ( String s[])

    {

        int a;

        for (a = 1; a <= 10; a ++)

         {

             System.out.print ( " hello"+ a);

                }

          }
}

 

While Loop Statements

While loop is a type of loops that allows executing statement after checking the condition. We manage the looping instructions into the different statements. The condition check with while keyword.

 

Flowchart:-

 

Image Source:- http://3.bp.blogspot.com

 

Syntax:-

Starting of  loop;

while (condition)

{

     statement;

     step of loop;

       }

 

Image Source:- http://img.c4learn.com

 

Example:-

Class A

{

    public static void main (String s[])

     {

         int a = 1;

         while (a <= 10)

          {

               System.out.print ("hello " + a);

                a ++;

                 }

            }

}

 

Nested For loop Statements

Nested For loop is a process to arrange the looping statements into an inner and outer format. The for loop use inside another loop as nested and control by its outer loop.

 

Flowchart:-

Image Source:- https://i.ytimg.com

 

Syntax:-

for ( starting of the loop; ending with the condition; a step of the loop)

{

    for ( starting of the loop; ending with the condition; a step of the loop)

     {

         statement;

           }

      statement;

}

 

Example:-

Class A

{

    public static void main (String s[])

     {

        int a, b;

        for ( a = 1; a <= 10; a ++)

         {

            for ( b = 2; b <= 5; b ++)

             {

                 System.out.print ( a*b + "/t");

                   }

             System.out.println ();

               }

        }

}

Nested while Loop Statements

Nested while loop is a type of loops way to arrange the looping statement into a nested format as while loop. The while loop use inside another while loop as an inner and outer format.

 

Flowchart:-

Image Source:- http://slideplayer.com

 

Syntax:-

starting of while loop ( ending with condition)

{

   starting of a loop;

    while ( ending with condition )

     {

         statement;

         step of loop;

           }

      statement;

       step of loop;

 }

 

Example:-

Class nested

{

   public static void main (String s[])

   {

       int a = 1, b = 2;

        while (a < 10)

        {

            while (b <= 10)

            {

                System.out.print (b*a + "/t");

                 b++;

                   }

           System.out.print ();

          a++;

            }

      }

}

 

Do-while Loop Statements

Do-while is a type of loops that executes the statements before checking the condition. It means it executes the instruction at least one time. The looping start with do keyword and while is used for checking the condition.

 

Flowchart:-

Image Source:- https://cdn.journaldev.com

 

Syntax:-

starting of a loop;

do

{

   statement;

   the step of a loop;

      }

while ( condition );

Example:-

Class A

{

   public static void main (String s[])

   {

      int a = 1;

      do

       {

           System.out.print (a);

           a++;

             }

       while (a <= 10);

         }

}

 

About Udit Mutreja

Check Also

Inheritance in Java

Inheritance in Java Inheritance is the feature of object-oriented programming concept for extending the data member …

Leave a Reply

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