While loop
The while loop repeats a statement or block of statements until the controlling condition is true.
Syntax:
While(condition){
// body of loop
}
The condition has to be a boolean expression.
Here is an example that calculates sum of numbers 1 to 10.
int i=1, sum=0; while(i<=10){ sum+=i; i++; } System.out.println("Sum = "+ sum);
While loop without a body
The body of while loop can be empty since a null statement is syntactically valid in java.
Here is an example of while loop without a body.
int i=10,j=20; while(++i<--j); System.out.println("Midpoint is " + i);
© 2015 – 2016, https:. All rights reserved. On republishing this post, you must provide link to original post