Home > Java > javaTutorial > do-while loop in Java

do-while loop in Java

WBOY
Release: 2024-08-30 15:25:08
Original
670 people have browsed it

Looping in any programming language has been used ever since. Loops and iterations form an essential component of the programming language, Be it Java or Python; One such looping construct is the do-while loop in the language of Java which is also popularly known as a post-incremental loop, i.e. where the looping construct runs for one time for sure and then the condition is matched for it to run the next time and so on. The condition, in this case, is put in the end. In other words, the condition blocks keep on executing continuously unless and until a specific set of conditions is termed as true.

Syntax: 

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

do
{
//block of statements
} while (expression);
Copy after login

The expression which is mentioned right after the while block is used to return a Boolean value, i.e. either a true value or a false value. If either of the values could not be found, then the compiler throws a compile-time error. The do command in this syntax ensures that the code is executed at least once, even when the expression is not executed, or the condition is not checked. The block of statements is the set of statements that are executed inside the do-while looping construct. This consists of the program body. The while statement, in the end, is used to evaluate the expression and then apply a postcondition to check whether the intended case is meeting the requirements and should be further looped.

How does a do-while loop work in Java?

Following is the explanation for how does do-while loop work in Java:

  • For a do-while loop to work, the condition is not necessary to be met as this loop also works well for the first time even when the condition is not met. The compiler executor then enters the function execution block executes whatever is there within the block of statements, and then comes out to check the expression part where the condition is compared. If the condition is met, then the loop is reiterated; otherwise, the loop is exited from the block. The basic difference between the while and the do-while loop is that while the former one looks for the pre-conditions, the latter one targets the postconditions.
  • The basic difference between a do-while and a very well-known loop is that the number of iterations is needed to be known in the loop and the initial value and the value which is being incremented. This is more often used when the iterations and their count are fixed in number, whereas in the case of the do-while loop, the number of iterations is not known before-hand but can change dynamically.

Flow Diagram:

do-while loop in Java

Examples for do-while loop in Java

Below are the examples of all the numbers till 10:

Example #1

Printing all the Numbers less than equal to 10.

Code:

public class Main {
public static void main(String args [])
{
int c=1;
System.out.println("printing all the numbers till 10:");
do
{
//printing all the numbers till 10
System.out.println(c);
++c;
} while(c<11);
}
}
Copy after login

Output:

do-while loop in Java

Example #2

Iterating an Array by making use of do-while loop in Java.

Code:

public class Main
{
public static void main(String args [])
{
int arr[]= {1, 6, 7, 514, 4, 98};
System.out.println("Printing the list of arrays: ");
//i in this case starts with 0 as the index in any looping statement has to start with the index of 0.
int i=0;
do
{
System.out.println(arr[i]);
++i;
} while (arr[i]<=6);
}
}
Copy after login

Output:

do-while loop in Java

Example #3

Writing a program for an Infinite do-while loop.

Code:

public class Main
{
public static void main(String[] args)
{
do
{
System.out.println("infinite do while loop");
} while(true);
}
}
Copy after login

Output:

do-while loop in Java

This program will run infinitely until the code block is explicitly broken as the infinite loop hits the condition of the while loop is true, which is a true condition and will always be met. Therefore, these types of loops and programming constructs are not encouraged. They can confuse the compiler and hang your system if it doesn’t contain appropriate RAM and other memory requirements.

Example #4

Printing all the Numbers from 10 till 1 in Reverse order.

Code:

public class Main
{
public static void main(String args [])
{
int c=10;
System.out.println("Printing all the numbers from 10 till 1");
do
{
//printing all the numbers till 10
System.out.println(c);
--c;
} while(c>0);
}
}
Copy after login

Output:

do-while loop in Java

Example #5

Printing all the numbers less than Equal to 10 without using the Increment operator.

Code:

public class Main
{
public static void main(String args [])
{
int c=1;
System.out.println("printing all the numbers till 10");
do
{
//printing all the numbers till 10 using shorthand operator
System.out.println(c);
c+=1;
} while(c<11);
}
}
Copy after login

Output:

do-while loop in Java

The basic difference between this example and the first example was the use of shorthand operators in this case and the use of a pre-incremental counter in the case of example number 1. Both of them will produce the same result; it is just a matter of your choice of which operator you wish to choose for the do-while loop.

Conclusion

In this post, we saw the basic level of introduction to do while loop. We also saw the working of the do-while loop in detail. We saw the intricacies and the major differences and the do-while loop applications over other looping constructs such as while or for loops. We studied the flowchart of the do-while loop, which helped us in understanding it more deeply. We read about the syntax and a huge list of various examples to understand the do-while loop clear.

The above is the detailed content of do-while loop in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template