• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo ยป Java ยป Java Tutorial ยป Do While Loop in Java with example

Do While Loop in Java with example

Last Updated on: October 20, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Do while loop Syntax
  • Infinite do-while loop
  • Do While Vs While loop

Java Do while loop: in the previous post of our softwaretestingo blog we have discussed the while loop and In This post, we are going to discuss do..while loop. the do-while loop is some similarities to while loop. But the difference between while and do-while loop is, in while loop condition is evaluated first if the condition returns true then only the loop’s body will be executed. Still, in the do-while loop, the body of the loop is executed first after that the condition evaluated.

Do while loop Syntax

the syntax for do-while loop looks like below:

do
{
 // statements 
} while (expression);

Note: The expression for the do-while loop must return a boolean value that is true or false; otherwise, you will get a compile-time error.

If you have noticed that the boolean expression is at the end of the loop, so before evaluating, we have run the statements for once. After that, only we evaluating the expression if the expression returns true, the flow jumps to the statements, and again the loop body statements will be executed. This process will run until the expression returns false.

package java_Basics;

public class DoWhileLoop_Example 
{
   public static void main(String[] args) 
   {
      int i = 5;
      do 
      {
         System.out.println(i);
         i++;
      } while (i <= 10);
   }
}

Output:

5
6
7
8
9
10

Infinite do-while loop

If the expression always returns a true value, then the loop will run for indefinitely times. In that case, we have to quit the loop manually by pressing the ctrl+c.

package java_Basics;

public class DoWhileInfiniteLoop_Example 
{
   public static void main(String[] args) 
   {
      int x = 100;
      do 
      {
         System.out.println("This is an infinite loop" );
         x++;
      }while( x > 10 );
   }
}

Do While Vs While loop

If your requirement is to execute at least once the statements of the loop body area, even the conditional expression returns false then that time it is recommended to use a do-while loop. Otherwise its always better to use while loop. Because the Java while loop it looks cleaner than the do-while loop.

Ref: Article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy