JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java Do-While Loop


A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time. The difference between do-while and while loop is that do-while evaluates its condition at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. Here is the syntax to write do-while loop:

do {
         statement(s)
} while (expression);

The do-while statement evaluates expression, which must return a boolean value. If the expression is true, the flow of control goes back to the do, and the statements within the loop executes again. This process repeats until the expression returns false.

Here is a simple do-while example:

package com.java2novice.loops;

public class SimpleDoWhileEx {

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

Output:
0 1 2 3 4 5 6 7 8 9 
<< Previous Program | Next Program >>

Java Loop Examples

  1. Java While Loop
  2. Java Do-While Loop
  3. Java For Loop
  4. Java For each Loop
  5. break statement in java
  6. continue statement in java
Knowledge Centre
What is java static import?
By using static imports, we can import the static members from a class rather than the classes from a given package. For example, Thread class has static sleep method, below example gives an idea:

import static java.lang.Thread;
public class MyStaticImportTest {
public static void main(String[] a) {
try{
sleep(100);
} catch(Exception ex){

}
}
}
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.