While loop in java with example

Previous Code:-
for loop

List Code:-
Java codes

Next Code:-

do-while loop


Java Programs:-
List of Java Programs

While loop is simplest loop in java

Syntax:-

while(condition)
{
Body of the loop
}
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.

Execution:-

In while loop 1st condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

Example:-

Program to count n number

import java.util.Scanner;

class group{
public static void main(String angr[]){
Scanner num = new Scanner(System.in);
int n, i;
System.out.println("hey enter number");
n=num.nextInt();
i=0;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}

5 Comments

Add your comments here

Back to Top