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);
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.
Following is the explanation for how does do-while loop work in Java:
Flow Diagram:
Below are the examples of all the numbers till 10:
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); } }
Output:
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); } }
Output:
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); } }
Output:
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.
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); } }
Output:
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); } }
Output:
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.
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!