While
What is a while loop?
While loop is a pre-test loop which means the condition will be first checked then the statement will execute, the while loop repeats the statement till the condition is true. When the condition is false then execution comes out from the loop.
Syntax:
while (condition){
// code to be executed
increment/decrement statement; // i++
}
Example:
package quipohouse;
class WhileLoop {
public static void main(String args[]) {
// initialization expression
int i = 1;
// test expression
while (i < 4) {
System.out.println("Quipo House");
i++; // update expression
}
}
}
Output:
Quipo House
Quipo House
Quipo House
Key Point
- In the while loop, based on the condition block statement is executed.
- it is also known as an entry control loop.