Question about bool

Hello,
I have a quick question about the bool from an example code in a book about c++ that i'm reading.
Here, the bool is set as 'done = false', however in the while statement it says 'while (!done)' which means if true then do the following (?) I think. And in order to get out of the loop you need to enter 999 which makes 'done = (input == 999)' which in turn would make done equal to true.

What i'm wondering is how does this not loop infinitely? Since the while loop executes when done is true, then how could you get out of the loop when done also equals true inside of the while loop?

Maybe i'm missing something really obvious or i'm thinking about it wrong and if somebody could explain I would be really grateful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main() {
    
    int input, sum = 0;
    bool done = false;
    while (!done) {
    std::cout << "Enter positive integer (999 quits): ";
    std::cin >> input;
    
    if (input < 0)
        std::cout << "Negative value " << input << " ignored\n";
    else
        if (input != 999) {
            std::cout << "Tallying " << input << '\n';
            sum += input;
    }
    else
     done = (input == 999);  // 999 entry exits loop
    }
    std::cout << "sum = " << sum <<'\n';

}

Here, the bool is set as 'done = false', however in the while statement it says 'while (!done)' which means if true then do the following (?) I think.
while (!done) means as long as done isn't true, keep doing the following. It's equivalent to while( done != true ).

And in order to get out of the loop you need to enter 999 which makes 'done = (input == 999)' which in turn would make done equal to true.

That is correct.
Topic archived. No new replies allowed.