Skip to main content

Java while loop

Java while loop


Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax:
while (condition(s)) {
// Body of loop
}
1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates to true or false and if it is a constant, for example, while (c) {…}, where c is a constant, then any non zero value of c is considered to be true, and zero is considered false.
2. You can test multiple conditions such as
  1. while (> b && c != 0) {
  2.  // Loop body
  3. }
Loop body is executed till value of variable a is greater than value of variable b and variable c isn't equal to zero.
3. A body of a loop can contain more than one statement. For multiple statements, you need to place them in a block using {}, and if the body of a loop contains only one statement, you can optionally use {}. It is always recommended to use braces to make your program easy to read and understand.

Java while loop example


Following program asks a user to input an integer and prints it until the user enter 0 (zero).
  1. import java.util.Scanner;
  2.  
  3. class WhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.     System.out.println("Input an integer");
  9.    
  10.     while ((= input.nextInt()) != 0) {
  11.       System.out.println("You entered " + n);
  12.       System.out.println("Input an integer");
  13.     }
  14.    
  15.     System.out.println("Out of loop");
  16.   }
  17. }
Output of program:

Java while loop break program


We can write above program using a break statement. We test a user input and if it is zero then we use "break" to exit or come out of the loop.
  1. import java.util.Scanner;
  2.  
  3. class BreakWhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.    
  9.     while (true) { // Condition in while loop is always true here
  10.       System.out.println("Input an integer");
  11.       n = input.nextInt();
  12.      
  13.       if (n == 0) {
  14.         break;
  15.       }
  16.       System.out.println("You entered " + n);
  17.     }
  18.   }
  19. }

Java while loop break continue program


  1. import java.util.Scanner;
  2.  
  3. class BreakContinueWhileLoop {
  4.   public static void main(String[] args) {
  5.     int n;
  6.    
  7.     Scanner input = new Scanner(System.in);
  8.    
  9.     while (true) {
  10.       System.out.println("Input an integer");
  11.       n = input.nextInt();
  12.      
  13.       if (n != 0) {
  14.         System.out.println("You entered " + n);
  15.         continue;
  16.       }
  17.       else {
  18.         break;
  19.       }
  20.     }
  21.   }
  22. }
Continue statement takes the control to the beginning of the loop and body of the loop executes again. Whatever you can do with a while loop can be done with a for loop or a do while loop.


Comments

Popular posts from this blog

Java program to print multiplication table

Java program to print multiplication table of a number entered by a user using a  for  loop. You can modify it for  while  or  do while  loop for practice. Using nested loops (a loop inside another loop), we can print tables of numbers between a given range say a to b, for example, if the input numbers are '3' and '6' then tables of '3', '4', '5' and '6' will be printed. Java programming source code import   java.util.Scanner ;   class   MultiplicationTable {     public   static   void   main ( String   args [ ] )     {       int   n, c ;       System . out . println ( "Enter an integer to print it's multiplication table" ) ;     Scanner in   =   new   Scanner ( System . in ) ;     n   =   in. nextInt ( ) ;       System . out . println ( "Multiplication table of "   + ...

Java program to print alphabets

Java program to print alphabets Java program to print alphabets on screen, i.e., a, b, c, ..., z; in lower case. Java source code class  Alphabets {     public   static   void  main ( String  args [ ] )     {        char  ch ;          for   ( ch  =   'a' ;  ch  <=   'z' ;  ch ++ )           System . out . println ( ch ) ;     } } You can easily modify the above java program to print alphabets in upper case. Download  Alphabets  program class file. Output of program: Printing alphabets using a while loop (Only the body of the main method is shown): char  c  =   'a' ;   while   ( c  <=   'z' )   {    System . out . println ( c ) ;   c ++; } Using a do while loop: char  c  =...

Use phone camera as a webcam without root in Hindi/urdu.