107k views
13 votes
what advantage is procided by using a boolean found variable in a compound condition for a search loop

User Heez
by
7.3k points

1 Answer

13 votes

Answer:

- the notation while(!found) { ... } is semantically very clear

- you can initialize the found boolean as false

Step-by-step explanation:

Within the while loop you will assign the compound condition to the 'found' variable. By initializing the variable with 'false' you ensure that the values used in the compound condition are valid.

Simplified example: read user input until 'Y' is pressed:

found = false;

while(!found) {

userInput = readUserInput();

found = (userInput == 'Y');

}

User Sam Van Herwaarden
by
7.6k points