Java Loops: for, while do-while

07 Feb 2023
Beginner
520 Views

Java Loops: for, while do-while

Loops are very time-saving and save the efforts of programmers. It assists the task performed repeatedly so that the programmers can write the code easily and again. This particular statement is called a loop because it works in a loop in order to complete one specific task.

What are loops in Java?

"Loops or iteration statements" are one of the most important parts of the control structure in Java."Loop statement" is used to repeat block statements for doing certain tasks within a certain time period. Looping statements in java is continuous execution until the evaluation of the expression turns out to be false.

Types of Loops in Java

There are 3 types of "Loops" in Java. These are "while loop", "do while loop", and "for loop" which will be discussed further.

For loop in Java

  • For loop primarily depends on the three major sections index of the control statement which is the "declaration", "conditions" and "updates" statement.
  • This particular loop statement generates iteration over a "collection of values". That can be the same value or can be different.
  • For loop is generally used when the user knows the exact number of iterations.
  • There are various types of "For loop" which are "Empty For Loop", "Infinite For Loop", and "For each loop".

Java For loop Syntax

for (initialization; termination condition; increment/decrement)
{
//Set of statements to be executed repeatedly
}

Example

//Program to print even numbers between 1-10.

class ForLoopDemo
{
 public static void main(String[] args)
    {
      for (int i=1; i<=10; i++)
        {
          if (i%2==0)
          System.out.println(i);
        }
          System.out.println("Loop Ending");

    }
}

Output

2

4

6

8

10

Loop Ending

While loops in Java

  • While Loop is also an iteration statement that can iterate over various types of values.
  • After accepting the condition While loop evaluates it to a "boolean value".
  • Execute a particular statement or a block of the statement until it generates "false".
  • While loop uses when the user is not aware of the exact iteration number.
  • There are various types of while loops; those are "Empty While Loop", and "Infinite While Loops".

Java While loop Syntax

//initialization
while (condition)
{
//Execute a set of statements
//increment
}

Example

class While_Loop_Demo
{
    //print even numbers ranging from 1 to 10

    public static void main(String args[])
  {
    int i = 1; //initialization
      while (i<=10) //condition or termination
      {

        if (i%2==0)
          {
              System.out.println(i);
          }

        i++; //increment
      }

  }
}

Output:

2

4

6

8

10

Do..while loops in Java

  • Do while loop works the same as while loop but it only generates the expression only after the execution of the code block has been done.
  • This statement first executes the statement under "do" after that it checks the condition of "while".
  • This statement executes at least once even though the statement generates a "false" value.
  • Do while loop is basically an "exit control loop".
  • There is only one type of “Do while loop” which is called "Infinite Do While Loop".

Java Do-While loop Syntax

do
{
//statements to be iterated
}
while(conditions);

Example

class Do_While_Loop_Demo
{
    //print even number
    public static void main(String args[])
  {

    int i = 1; //initialization
    do
      {
         System.out.println(i);

         i++; //increment
      } while (i%2==0);//condition or termination

  }
}

Output

1

2

Summary

Java loops can be used in a variety of ways to optimize code performance meaning that implementation is dependent on individual program needs. Now that you've brushed up on your knowledge of both while and do-while loop concepts, it's time to get out there and start experimenting with different techniques for yourself! Java Loops open a world of possibilities for you as a coder and countless opportunities for creativity – what will you create first?

Accept cookies & close this