Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Difference between While and Do While in Java

by suresh

What are the difference between While and Do While in Java Programming Language with a practical example?

Difference between While and Do While in Java

Although Do While loop and While loop in Java looks similar, they differ in the order of execution.

  • In Java While loop, the condition is tested at the beginning of the loop, and if the condition is True, then only statements in that loop will be executed. So, the While loop executes the code block only if the condition is True.
  • In Java Do While loop, the condition is tested at the end of the loop. So, the Do While executes the statements in the code block at least once even if the condition Fails.

Maybe you are confused, and I think you will understand it better when you see the example. Let us write the same program using Java While loop and Do While loop to understand the order of execution.

Java While loop example

In this Java program, we are declaring integer variable Number and assigned value zero to it. Next, We will check whether Number (value = 0) is greater than ten or not to fail the condition deliberately. There is one more System.out.println statement outside the While loop and this statement will execute after the while loop.

package Loops;

public class WhileLoop {
	
	public static void main(String[] args) {
		int number = 0;
		
		while (number > 10)  {
			System.out.println("Number is Greater Than 10");
		}
		System.out.println("This Statement is Coming from Outside of while loop");
	}
}
Difference between While and Do While in Java 1

Java Do While Loop example

In this program, We are going to write the same example using Do while loop

package Loops;

public class DoWhileDiff {
	
	public static void main(String[] args) {
		int number = 0;
		
		do {
			System.out.println("Number is Greater Than 10");
		}while (number > 10);
		System.out.println("This Statement is Coming from Outside of while loop");
	}
}
Difference between While and Do While in Java 2

Although the condition fails, the statement inside the loop executed once. Because the do while loop condition tested after the execution of the statements.

We hope you understood the difference :)

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy