Help understanding this program flow

I've been looking at code recently to try to get better at reading how it all flows together when I came across this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  /* The problem:
    Add a unit to each double entered; that is, enter values such as 10cm,
    2.5in, 5ft, or 3.33m. Accept the four units: m, cm, in, ft. Assume
    conversion factors 1m == 100cm, l in == 2.54cm, ft == 12in. Read the unit
    indicator into a string. */


#include "../../std_lib_facilities.h"

int main()
{
    double a;
    string unit = "";
    double smallest = 0;
    double largest = 0;
    bool initialize = false;

    while (cin >> a >> unit)
    {
        cout << "You have entered: " << a << unit << endl;

        if (unit == "m")
            a = a * 100;
        else if (unit == "cm")
            a = a * 1;
        else if (unit == "ft")
            a = a * 12 * 2.54;
        else if (unit == "in")
            a = a * 2.54;

        /*no comparison until second set of values are accepted. That is when
            trackers of largest and smallest numbers are initialized*/

        if (!initialize)
        {
            smallest = a;
            largest = a;
            initialize = true;
        }
        else
        {
            if (a > largest)
            {
                cout << "The largest so far" << endl;
                largest = a;
            }

            if (a < smallest)
            {
                cout << "The smallest so far" << endl;
                smallest = a;
            }
        }
    }
    return 0;
}


The part that confuses me is the first if statement that begins the comparisons.

1
2
3
4
5
6
7
8
9


        if (!initialize)
        {
            smallest = a;
            largest = a;
            initialize = true;
        }
 


What's happening here? Doesn't this mean "if NOT false perform this"? initialize is initialized as false, so shouldn't this be completely skipped? And if not false is true why would it need to change the bool to true in the statement?
The unary operator "!" (logical NOT) returns the negation of the argument. In other words, if you write the following:

bool flag = !true; //flag is false

bool flag = !false; //flag is true

So, when you have an if statement like this:

1
2
3
4
5
bool is_initialized = false;

	if (!is_initialized) {
		//...
	}


The "!" negates whatever is_initialized is. Since is_initialized is false at this point in time, the expression (!is_initialized) is equivalent to (!false), which is (true). For this reason, code execution enters the if branch.


why would it need to change the bool to true in the statement?


This if statement sits inside of a loop, and it contains code that should be executed once and only once, since it's responsible for the initialization of something. Since we're in a loop, it could be that that "something" gets initialized more than once, which we don't want! The code in the if branch is only executed the first time it is evaluated, since initialize is initialized to false. In the if branch, the last thing we do is set initialize to true so that we'll never enter this if branch again in any of the following iterations of the loop.
Last edited on
@xismn

so, is it basically the same as writing

1
2
3
4
5
if(initialized == false)
{
//....
initialized = true;
}


?
In this context, yes.
Topic archived. No new replies allowed.