import java.util.Scanner; // bring in Scanner class so we can use it public class While /** This class Introduces While Loop **/ // loop tells jvm to execute statements repeatedly { public static void main(String[] args) { /* general format of a while loop: * while (boolean expr) { body of while loop }**/ int num=1; while (num < 5) { System.out.println("hello world"); num++; } System.out.println("Counting down:"); int count=5; while (count > 0) { System.out.print(count+" "); count--; } // ask the user how many times Scanner keyboard = new Scanner(System.in); System.out.print("\nHow many times should loop run?"); count = keyboard.nextInt(); // read in count while (count > 0) { System.out.print(count+" "); count--; } System.out.println("\nNew loop, running until a sentinel: "); // using a sentinel value // and structured read loop int number=0; // number will hold the data value we read in System.out.print("Enter a number, -1 to exit: "); number = keyboard.nextInt(); while (number != -1) { // this while loop will read in numbers, and display them // until the user enters a -1 System.out.println("you entered: " + number); // in a structured read loop, the last thing you do // is read in value for NEXT iteration of the loop System.out.print("Enter a number, -1 to exit: "); number = keyboard.nextInt(); } } } /************** * LAB 7: * Write a while loop to read in names. Each time a name is read * in, we say hello to that person. * When user enters a . (dot) program terminates and prints Good Bye! * * sample output: * Enter a name: Alice * Hello Alice! * Enter a name: Bob * Hello Bob! * . * Good bye! *****************/ * * * * * * *