Using Flag-Controlled While Loops
Submitted by GeePee on Tuesday, May 19, 2015 - 22:49.
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
Sample Run:
prompts the user to enter an integer greater than or equal to 0 and less than 100.
The statement executes.
The statement outputs the message:
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:
- import java.util.*;
- public class FlagControlledLoop
- {
- {
- //declare varibles
- int num; //variable to store the random number
- int guess; //variable to store the number guessed by the user
- boolean done; //boolean variable to control the loop
- done = false;
- while (!done)
- {
- + "and less than 100:");
- guess = console.nextInt();
- if (guess == num)
- {
- done = true;
- }
- else
- if (guess < num)
- else
- }//end while
- }
- }
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- + "and less than 100:");
guess = 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- done = true;
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 else
executes.
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
- 2207 views