Java While Keyword

Java While Keyword


While Keyword In Java
Basically, it is used when how many times statements should be executed is unknown.  
As while is a predefined keyword it will always be in small case.
It is also known as entry controlled loop.
Syntax-
 
while(condition){

statement1;
statement2;
statement3;
statement4;

updation;

}
 
Example1 - Write a program that displays sum of digit.
 
import java.util.Scanner;
public class T
{
    public static void main(String[] args)
    {
Scanner ob=new Scanner(System.in);
int a,r,s=0;        
System.out.println("Enter digit");
a=input.nextInt();
  while(a>0)
      {
            r= a%10;
            a =a/10;
            s = s+r;
      }
System.out.println("Sum of Digits of Number is" +s);
      }          
  }
 
Example2 -Write a program that display first 100 natural number.
 
class T
{
public static void main(string[] args)
{ int i=1;
while(i<=100){
system.out.print(i);
i++;
}
}
 
Example3 - Write a program that take a number randomly and print its factorial.
 
class T
{
public static void main(string[] args)
{ Random random = new Random(100)
int i,fact=1;
i=random;
while(i>=1){
f = f*i;
i--;
}
system.out.print("factorial is ="+f);
}
}