Using Flag-Controlled While Loops

The following java program uses a Flag-Controlled While Loop. It uses a Boolean variable to control the loop. The variable such as found, which is used to control the execution of the while loop, is called flag variable. . I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code:
  1. import java.util.*;
  2.  
  3.         public class FlagControlledLoop
  4.         {
  5.         static Scanner console = new Scanner(System.in);
  6.         public static void main (String[] args)
  7.         {
  8.                
  9.         //declare varibles
  10.         int num; //variable to store the random number
  11.         int guess; //variable to store the number guessed by the user
  12.                
  13.         boolean done; //boolean variable to control the loop
  14.                
  15.         num = (int) (Math.random() * 100);
  16.         done = false;
  17.         while (!done)
  18.         {
  19.         System.out.print ("Enter an integer greater than or equal to 0 "
  20.                 + "and less than 100:");
  21.         guess = console.nextInt();
  22.         System.out.println();
  23.                
  24.         if (guess == num)
  25.         {
  26.         System.out.println("You guessed the correct number: ");
  27.                         done = true;
  28.                        
  29.         }
  30.         else
  31.         if (guess < num)
  32.         System.out.println("Your guess is lower than the number.\n Guess again!");
  33.         else
  34.         System.out.println("Your guess is higher than the number. \n Guess again!");
  35.                 }//end while
  36.         }
  37. }
Sample Run: Enter an integer greater than or equal to 0 and less than 100:25 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:5 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:4 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:3 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:2 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:1 Your guess is higher than the number. Guess again! Enter an integer greater than or equal to 0 and less than 100:0 You guessed the correct number: Process completed. The preceding program works as follows: The num = (int) (Math.random() * 100); creates an integer greater than or equal to 0 and less than 100 and stores this number in the variable num. The statement done = false; sets the Boolean variable done to false. The expression in the while loopwhile (!done)evaluates the expression !done If done is false, then !done is true and the body of the while loop executes, if done is true, then !done is false, so the while loop terminates. The statement
  1. System.out.print ("Enter an integer greater than or equal to 0 "
  2.                                 + "and less than 100:");
prompts the user to enter an integer greater than or equal to 0 and less than 100. The statementguess = console.nextInt(); stores the number entered by the user in the variable guess The expression in the if statement determines whether the value of guess is the same as num, if the user guessed the number correctly. If the value of guess is the same as num, then the statements
  1. System.out.println("You guessed the correct number: ");
  2.                         done = true;
executes. The statement outputs the message: Your guessed the correct number. The statement        done = true;sets the variable done to true. The control then goes back to         while (!done) If the expressionif (guess == num) evaluates to false, then the else statement elseexecutes. The if statementif (guess < num) determines whether the value of guess is less than num. the statementSystem.out.println("Your guess is lower than the number.\n Guess again!"); outputs the message: Your guess is lower than the number. Guess again! If the expression in the if statement in if (guess < num)evaluates to false, then the statementSystem.out.println("Your guess is higher than the number. \n Guess again!");executes, which outputs the message: Your guess is higher than the number. Guess again! The program then prompts the user to enter an integer greater than or equal to 0 and less than 100.

Add new comment