• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
EDUCBA

EDUCBA

MENUMENU
  • Resources
        • Java Tutorials

          • Cheat Sheet Java
          • Cheat Sheet Python
          • C# vs Js
        • Java Tutorials
        • Python Tutorials

          • Angular 5 vs Angular 4
          • Careers in Python
          • Kali Linux vs Ubuntu
        • Python Tutorials
        • Top Differences

          • Cheat Sheet JavaScript
          • Python Interview Questions
          • Cloud Computing or Virtualization
        • Top Differences
        • Others

          • Resources (A-Z)
          • Top Interview Question
          • Programming Languages
          • Web Development Tools
          • HTML CSS Tutorial
          • Technology Basics
          • Technology Careers
          • View All
  • Free Courses
  • All Courses
        • Certification Courses

          Software Development Course 2
        • All in One Bundle

          All-in-One-Software-Development-Bundle
        • Become a Python Developer

          Python-Certification-Training
        • Others

          • Java Course
          • Become a Selenium Automation Tester
          • Become an IoT Developer
          • Ruby on Rails Course
          • Angular JS Certification Training
          • View All
  • 600+ Courses All in One Bundle
  • Login

do-while loop in Java

Home » Software Development » Blog » Java Tutorials » do-while loop in Java

Do while loop in java

Introduction to do while loop in Java

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 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.

How does a do while loop work in Java?

Following are 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 case of the loop along with 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 on java

Examples for do-while loop in Java

Below are the examples of all the number 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);
}
}

Output:

while loop 1

Example #2

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

Popular Course in this category
Java Training (38 Courses, 26 Projects) 38 Online Courses | 26 Hands-on Projects | 274+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.8 (3,556 ratings)
Course Price

View Course

Related Courses
JavaScript Certification Training (39 Courses, 20 Projects)jQuery Training Training (8 Courses, 3+ Projects)

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:

while loop2

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);
}
}

Output:

while loop3

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 this type of loops and programming constructs are not encouraged as they can confuse the compiler and can 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);
}
}

Output:

while loop 4

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);
}
}

Output:

while loop5

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 the matter of your choice which operator you wish to choose for the do-while loop.

Conclusion

In this post, we discussed the basic level of introduction to do while loop. We also discussed the working of the do-while loop in detail. We discussed the intricacies and the major differences along with the applications of the do-while loop 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 also a huge list of various examples to make the understanding of the do-while loop clear. I hope you liked this article. Stay tuned with us for more articles like these.

Recommended Articles

This is a guide to do while loop in Java. Here we discuss how it does do while loop in Java in Java, with flow diagram and top5 examples in it. You can also go through our other related articles to learn more –

  1. VB.Net for Loop
  2. Loops in Java Programming
  3. PHP Do While Loop
  4. While Loop in Matlab
  5. Reverse in JavaScript
  6. Arrays in Java Programming
  7. Do While Loop in JavaScript
  8. VB.Net Events

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Reader Interactions
Leave a Reply Cancel reply

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

Primary Sidebar
Technology Blog Tutorials
  • Java Tutorials
    • Hashing Function in Java
    • JavaFX Charts
    • JavaFX Applications
    • 2D Graphics in Java
    • Final Keyword in Java
    • Hibernate Framework
    • JavaFX Slider
    • jQuery querySelector
    • JProgressBar
    • Jslider
    • Print 2D Array in Java
    • JavaFX Button
    • JavaFX TextField
    • Regular Expressions in Java
    • JavaFX FileChooser
    • HashMap in Java
    • HashMap in Java
    • JTree in Java
    • JTextPane
    • What is JavaFX?
    • Iterator in Java
    • jQuery Elements
    • Hibernate Session
    • JavaFX Text
    • Slide in jQuery
    • jQuery plugins
    • Type Conversion in Java
    • Versions of Java
    • Overriding in Java
    • Overloading and Overriding in Java
    • Static Keyword in Java
    • Java Naming Conventions
    • Object in Java
    • Polymorphism in Java
    • Quick Sorting Algorithms in Java
    • JavaFX Label
    • Overloading in Java
    • JavaFX Layouts
    • Multidimensional Array in Java
    • JLabel in Java
    • Super Keyword in Java
    • Arrays in Java Programming
    • Java Booleans
    • Flowlayout in Java
    • Array Methods in Java
    • GridBagLayout in Java
    • JTextArea in Java
    • Protected Keyword in Java
    • JPopupMenu
    • JTabbedPane in Java
    • Inheritance in Java
    • Java Keywords
    • Fibonacci Series in Java
    • Java Deployment Tools
    • What is Angular 2?
    • For-Each loop in Java
    • Wrapper Class in Java
    • JFileChooser in Java
    • JFrame in Java
    • GroupLayout in Java
    • SpringLayout in Java
    • JToggleButton
    • Random Number Generator in Java
    • Recursion in Java
    • JList in Java
    • JColorChooser
    • JUnit Annotations
    • Squares in Java
    • JCheckBox in Java
    • Break Statement in Java
    • do-while loop in Java
    • Continue Statement in Java
    • JComponent in Java
    • Insertion Sort in Java
    • Palindrome in Java
    • Program for Merge Sort in Java
    • What is JNDI in Java?
    • Swing Components in Java
    • Java Virtual Machine
    • JMeter Alternatives
    • Sorting Algorithms in Java
    • Sorting in Java
    • JOptionPane in Java
    • Bubble Sort in Java
    • JEditorPane
    • Jquery IF Statement
    • For Loop in Java
    • Factorial in Java
    • Jspinner
    • JButton in Java
    • What’s New in Java 8?
    • Singleton Class in Java
    • JPanel in Java
    • Swapping in Java
    • jQuery Attributes
    • Java Annotations
    • JDialog in Java
    • JTextField in Java
    • JScrollPane in Java
    • 3D Arrays in Java
    • What is Java Garbage Collector?
    • Constructor in Java
    • Math Functions in Java
    • CardLayout in Java
    • Socket Programming in Java
    • Print Array in Java
    • Merge Sorting Algorithms in Java
    • StringBuffer in Java
    • JComboBox in Java
    • Reverse Number in Java
    • Square Root in Java
    • BorderLayout in Java
    • Java Packages
    • Quick Sort in Java
    • 2D Arrays in Java
    • Access Modifiers in Java
    • Case Statement in Java
    • Patterns in Java
    • Variables in Java
    • Encapsulation in Java
    • Selection Sort In Java
    • Types of Selector in Jquery
    • What is Java SE?
    • Abstract Class in Java
    • Thread Life cycle in Java
    • Loops in Java Programming
    • BoxLayout in Java
    • Gridlayout in Java
    • Destructor in Java
    • Heap Sort In Java
    • Bit Manipulation in Java
    • Best Java IDE
    • What is Servlet?
    • Best Java Compilers
    • Star Patterns in Java
    • While Loop in Java
    • Constructor and Destructor in Java
    • Merge Sort In Java
    • Copy Constructor In Java
    • What is Java Interface?
    • Frameworks In Java
    • Layout in Java
    • What is Synchronization in Java?
    • Testing Frameworks for Java
    • Java Tools
    • Jenkins Plugins
    • Java Compilers
    • JSP Directives
    • JSP Life Cycle
    • JSP Architecture
    • Java Programming Language Features
    • Servlet Life Cycle
    • Object Oriented Programming in Java
    • Applications of Java
    • Careers as Java Developer
    • String Functions in java
    • Is Javascript Case Sensitive
    • Jenkins Alternatives
    • What is Java Inheritance
    • Best Javascript Frameworks
    • Redux Alternatives
    • Grid Layout in Java
    • What is a Binary Tree in Java
    • Java App Development
    • What is Hibernate
    • What is Design Pattern in Java
    • What is API in Java
    • Java and JavaScript
    • Is Jenkins Free
    • What is JDK
    • What is JVM
    • What is AWT in Java
    • What is Swing
    • What is JSF
    • What is Spring Boot
    • What is Generics in Java
    • What is EJB
    • What is Jenkins
    • What is Java Hibernate
    • What Javascript Can Do
    • What is Concurrency in Java
    • What is Multithreading in java
    • What is JavaBeans
    • What is Redux
    • Template in Java
    • What is J2EE
    • JDBC Architecture
    • Cheat Sheet Java
    • Uses of Jenkins
    • What is Swift
    • Serialization in Java
    • What is JNI in Java
    • What is JSP
    • Uses of JavaScript
    • JSF Life Cycle
    • What is Maven Plugins
  • Database Management (71+)
  • Ethical Hacking Tutorial (33+)
  • HTML CSS Tutorial (47+)
  • Installation of Software (54+)
  • Top Interview question (188+)
  • JavaScript (71+)
  • Linux tutorial (32+)
  • Network Security (85+)
  • Programming Languages (232+)
  • Python Tutorials (89+)
  • Software Development Basics (321+)
  • Software Development Careers (38+)
  • SQL Tutorial (33+)
  • String Functions (12+)
  • Technology Commands (38+)
  • Top Differences (368+)
  • Web Development Tools (33+)
  • Mobile App (60+)
Technology Blog Courses
  • Java Course
  • JavaScript Certification Course
  • jQuery Training Course
Footer
About Us
  • Who is EDUCBA?
  • Sign Up
  •  
Free Courses
  • Free Course Programming
  • Free course Python
  • Free Course Java
  • Free Course Javascript
  • Free Course on SQL
  • Free Course on Web Design
  • Free HTML Course
  • Free Android App Development Course
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • Ruby on Rails Course
  • ASP.NET Course
  • VB.NET Course
  • Bootstrap Training Course
  • Become a Linux System Administrator
  • PHP Course
  • Joomla Training
  • HTML Course
Resources
  • Resources (A To Z)
  • Java Tutorials
  • Python Tutorials
  • Top Differences
  • Top Interview Question
  • Programming Languages
  • Web Development Tools
  • HTML CSS Tutorial
  • Technology Basics
  • Technology Careers
  • Ethical Hacking Tutorial
  • SQL Tutorials
  • Digital Marketing
Apps
  • iPhone & iPad
  • Android
Support
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions

© 2019 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Let’s Get Started
Please provide your Email ID
Email ID is incorrect

Limited Period Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More

Limited Period Offer - Limited Period Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) View More