Need help understanding bool

I was working on some exercises from class until I got stuck on a question and decided to have a look at the answer. This was the solution.
1
2
3
4
5
6
7
8
9
10
11
12
13
  int position(int num, const int scores[]) {
	bool found(false);
	int pos(-1);
	int i(0);
	while ((!found) && (i < SIZE)) {
		if (scores[i] == num) {
			found = true;
			pos = i;
		}
		++i;
	}
	return pos;
}


The code runs fine, but I'm having trouble trying to understand why the while loop only terminates if found is set to true and not false. Shouldn't it terminate if its set to false? since then the condition would be false thus exiting? When I set it to false it loops infinitely. I've been stuck trying to figure out why for the last few hours now.

I can also provide the rest of the code if its necessary.
I'm having trouble trying to understand why the while loop only terminates if found is set to true and not false

while (!found) is saying "while(not found)", which is equivalent to saying "while(found == false)".
In other words, "while found is false, continue looping". So it will continue looping when set to false.

It's the condition inside the while () that needs to, as a whole, evaluate to true, for the while loop to continuing looping.
If found is false, then !found is true, and it will loop.
Last edited on
I get that the condition inside the while() needs to be true for it to loop, but the part thats confusing me is why it exits the loop when found is set to true inside the loop. Is it because the 'found' inside the loop and the condition '!found' are now the same state(true)? or is it something else that I'm not getting?
If found == true, !found will == false.

!true == false, !false == true

http://www.cplusplus.com/doc/tutorial/operators/

Scroll down for the logical operators section.
To be pedantic about this:
1
2
3
4
5
6
bool t( true );

while( t )
{
 t = false;
}


This "loop" will execute only once. "t" is tested each time the loop is executed. The first time, of course, t is true. "While" allows the code in the brackets to execute when true (which you've indicated you understand).

That code sets t to false. When the loop concludes that first pass, the execution point returns to the test inside the while. At that point, t was changed. Since it is now false, the loop terminates.

Now, per your example

1
2
3
4
5
6
7
bool t( false );

while( !t )
{
 if ( something is found ) t = true;
}


This is obviously pseudo code. "something is found" is written to indicate that this loop may repeat several times until the condition that something has been found is met, at which point t is set to true.

As you've already covered, in this case t starts as false. The ! operator inverts the test, so when t is false the ! inverts that to true, and so the code inside the loop executes.

That will repeat until something is found. How many times depends on how many steps are required to find something, but when it is, t is set to true.

The test at the top of the loop is repeated each time, and on that occasion that t is set to true the ! inverts the test, making the result false.

At which point the loop is no longer processed, and it is said that control exits the loop.

It may be important to expand one more illustration of this point.

1
2
3
4
5
6
7
8
bool t( false );

while( !t )
{
 if ( something is found ) t = true;
 somefunction();
}



In the interest of being clear (and thus pedantic), I point out that in this case even when something is found, and t is set to true, the function "somefunction()" will still be called before the loop restarts and t is tested again.

If for some reason that isn't appropriate, one might choose to call "break" when t is set to true. Break will stop the loop at the position the break is encountered, and no test will be conducted (break will stop the loop even if the test remains true - where in this case t is set to false). If "t = true" were replaced with "break", somefunction would not be called, t won't even be checked, but the loop terminates.

Last edited on
The test at the top of the loop is repeated each time, and on that occasion that t is set to true the ! inverts the test, making the result false.

That literally cleared everything up for me! For some reason I overlooked the fact the condition is checked every loop and didn't realise that found = true would get carried into the next iteration, meaning !found = false, thus loop terminates. Thank you so much!
Last edited on
The name "found" was clearly chosen to express what the variable represents. One could use other names:
1
2
3
4
5
6
7
8
9
10
11
12
13
int position( int num, const int scores[] ) {
  bool still_seeking {false};
  int pos(-1);
  int i(0);
  while ( still_seeking && (i < SIZE)) {
    if (scores[i] == num) {
      still_seeking = false;
      pos = i;
    }
    ++i;
  }
  return pos;
}

However, the entire variable is unnecessary; the function returns only position (or -1):
1
2
3
4
5
6
7
8
int position( int num, const int scores[] ) {
  for ( int pos = 0; pos < SIZE; ++pos ) {
    if ( scores[pos] == num ) {
      return pos;
    }
  }
  return -1;
}

The function does have a serious limitation though: it has to and can only seek SIZE elements. No more, no less.

What if I have two arrays, one with 7 elements and other with 42 elements?
Topic archived. No new replies allowed.