While Loop

A while loop statement in java programming language repeatedly executes a target statement as long as a given condition is true.

Key point of the while loop is that the loop might not ever run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Let's create a file with name "Whileloop.java" and try it out,

public class Whileloop {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }

}

With java.util.Scanner or User Input :-

Create a file with name "Bank.java"

import java.util.Scanner;

public class Bank
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);
		int pin = 90909;

		System.out.println("Welcome to Secret Bank of w3Clan.");
		System.out.print("Enter your PIN : ");
		int entry = keyboard.nextInt();

		while ( entry != pin )
		{
			System.out.println("\nINCORRECT PIN. TRY AGAIN.");
			System.out.print("ENTER YOUR PIN: ");
			entry = keyboard.nextInt();
		}

		System.out.println("\nPIN ACCEPTED. YOU NOW HAVE ACCESS TO YOUR ACCOUNT.");
	}
}

Compile and Run :-

c:\java> javac Bank.java
c:=java> java Bank

 


Loading ...

Related Results :

  1. While Loop
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author