Krivalar Tutorials 
Krivalar Tutorials

Java while loop in programming


<<Previous

Next >>





while statement

while is a conditional loop statement. The statements within the while block are executed only if the condition returns true.

while - Syntax


while(condition)
{
	statement;
}

while - Example


public class While {
public static void main(String a[]){
  int i=0;

  System.out.println(
		"\nbefore first while loop");
  while(i<10){
	System.out.print(
		"" + i + ",");
	i=i+1;
  }

  i=20;

  System.out.println(
		"\n\nbefore second while loop");
  while(i<10){
	System.out.print("" + i + ",");
	i=i+1;
  }
  System.out.println(
	"\nafter second while loop");
}
}

The above while loop prints:

before first while loop
0,1,2,3,4,5,6,7,8,9,

before second while loop

after second while loop


<<Previous

Next >>