While Loop in Java
The while loop is Java's most fundamental loop statement. It repeats a statement or block while its controlling expression is true.The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop.
- If the condition is true, the code inside the while loop is executed.
- The condition is evaluated again.
- This process continues until the condition is false.
- When the condition is false, the loop stops.
Syntax
while (Condition)
{
// body of loop;
// Increment (or) Decrement;
}Code Snippet
//While Loop in Java
import java.util.Scanner;
public class App {
public static void main(String args[]) {
System.out.println("Enter The Limit : ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int i = 1;
while (i <= n) {
System.out.println(i);
i++;
}
}
}