flag variables and break statement question.

closed account (jh5jz8AR)
I am going through my C++ book and got caught off guard here and was wondering if someone can explain the following:

1. What exactly is a (flag) variable.
2. The example they provide does not have a break statement. They just use a boolean to exit the program. Do they mean that the boolean is the break?


The use of a break statement in a loop can eliminate the use of certain (flag) variables. The following c++ code segment helps illustrate this idea.

(Assume that all variables are properly declared.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sum = 0;
isNegative = false;

cin >> num;

while (cin && !isNegative)
    {
    if (num < 0)	// If num is negative, terminate loop after this itt...
	    {
	        cout << "Negative number found in the data." << endl;
		isNegative = true;
	    }
	    else
	    {
	 	sum = sum + num;
		cin >> num;
	    }
    }


I believe isNegative would be considered the (flag) variable. The reason I believe this is because it is used to determine if the loop is true or false. Am I correct in thinking this?
Last edited on
Yes. It's just a boolean variable who's purpose is to determine whether something it true.
closed account (jh5jz8AR)
Hey firedraco,

Thank you for clarifying. The wording through me off a bit.
Topic archived. No new replies allowed.