TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Do…While Loop in Java
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java Flow ControlJava Tutorials

Do…While Loop in Java

Last updated: Sep 04, 2023 6:50 pm
By Meenakshi Agarwal
Share
4 Min Read
Java Do While Loop Tutorial
SHARE

This tutorial will guide you on how to use do while loop in Java programs, perform repetitive tasks, and iterate through the elements of a collection or array.

Contents
SyntaxFlowchartGenerate first N whole numbersCount backward from a given numberIterate through an array/collection

It is a core Java programming construct used to perform repetitive tasks. The tutorial has the following sections to help you learn quickly.

Java Do While Loop Tutorial

Basics of do while loop in Java

Unlike the while loop in Java which checks the condition at the entry, do…while allows the code block to execute at least once. After the first iteration, it evaluates the test condition and decides whether to continue or not.

Similar to other loops, it too accepts only those expressions which produce a boolean result. Please note that a non-zero integer value also evaluates to true.

Syntax

The do-while loop in Java places the condition at the entry gate.

do
{
    statement(s);
} while (boolean expression);
  • The do…while loop executes a block of code continuously until the given expression returns true.
  • It is more like a Java while loop except that the do-while guarantees the execution at least once.
  • It terminates the cycle immediately when the condition turns false.

e.g.

public class SimpleDoWhile {

   public static void main(String args[]){
        int iter = 0;
        do
        {
            System.out.print(iter + " ");
        } while (iter++ < 10);
   }
}

The above loop will run 11 times producing numbers from 0 – 10.

Output:

0 1 2 3 4 5 6 7 8 9 10

Flowchart

The flow chart will help you visualize the do-while loop in Java:

Java Do While Loop Flowchart

Do while examples

You can use the do-while loop for many purposes. Below are some samples to demonstrate it.

Generate first N whole numbers

public class doWhileLoop {
    public static void main(String args[]) {
        int n = 0;
        int N = 5;
        do
        {
            System.out.print(n + " ");
            n++; // Incrementing n by 1 in each iteration
        } while ( n <= N );
    }
}

In the above Java program, instead of writing the print statement for N times, we made the do-while loop resolve it. Here ‘n’ is the loop control variable and N has a fixed value.

The output is as follows:

0 1 2 3 4 5

Count backward from a given number

public class doWhileLoop {
    public static void main(String args[]) {
        int N = 5;
        do
        {
            System.out.print(N + " ");
            N--; // Decrementing N by 1 in each iteration
        } while ( N >= 0 );
    }
}

The result is as follows:

5 4 3 2 1 0

You can see that do…while loop is letting us manipulate the test condition and update the counter to produce different outputs.

Iterate through an array/collection

public class doWhileLoop 
{ 
    public static void main(String args[]) {
         char char_arr[] = {'a', 'b', 'c', 'd'};
         // array index begins with 0
         int iter = 0;
         do {
              System.out.print(char_arr[iter] + " ");
              iter++;
         } while( iter < char_arr.length );
    }
}

Test run:

a b c d

Please note that if you replace the array type in the above example from integer to char, then do…while loop will print the ASCII values of each char.

97 98 99 100

Must Read – For Loop in Java

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Java While Loop Tutorial While Loop in Java
Next Article Java Class and Object Concept Java Class and Object Explained

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x