Upskill faster and boost your growth in 2024 with our upto 50% OFF on All Courses! Offer Ending in
D
H
M
S
Get Now
Looping Statements in Java - For, While, Do-While Loop in Java

Looping Statements in Java - For, While, Do-While Loop in Java

05 Dec 2023
Beginner
1.53K Views
7 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming Course

Start Learning Free View All Courses

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. These particular looping statements in Java, as explained in a Java tutorial, are called this 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. Strings in Java, 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. The concept of Abstraction in Java can be applied to understand the Do-While loop better
  • 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

In this comprehensive Java tutorial article, Looping statements in Java 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, especially if you're undergoing Java Certification training, and offer countless opportunities for creativity. So, what will you create first as you dive into the exciting realm of Java loops during your Java Certification training?

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this