JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java While Loop


The while loop or while statement continually executes a block of statements while a particular condition is true. The while syntax can be written as:

while (expression) {
      statement(s)
}

The while loop evaluates expression, which must return a boolean value. If the while loop expression returns true, then the statements with in the while block will be executed. The while loop continuesly executes the statements within the block, until the expression returns false.

Here is a simple while loop example, which executes until i value became 10:

package com.java2novice.loops;

public class SimpleWhileEx {

	public static void main(String a[]){
		int i=0;
		while(i < 10){
			//this block will executed until
			//i value became 10
			System.out.print(i+" ");
			i=i+1;
		}
	}
}

Output:
0 1 2 3 4 5 6 7 8 9 
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 race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

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.