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.