The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details. |
![]() |
Home ![]() ![]() |
||
See also: Boolean Values, Bool Functions, Logical Operators | ||
![]() ![]() ![]() ![]() ![]() ![]() |
||
Boolean Variables
fred = true; bool testResult = false; The first line is a simple variable declaration; the second line is an assignment, and the third line is a combination of a declaration and as assignment, called an initialization. As I mentioned, the result of a comparison operator is a boolean, so you can store it in a bool variable bool evenFlag = (n%2 == 0); // true if n is evenbool positiveFlag = (x > 0); // true if x is positive and then use it as part of a conditional statement later if (evenFlag) {cout << "n was even when I checked it" << endl; } A variable used in this way is called a flag, since it flags the presence or absence of some condition.
|
||
Home ![]() ![]() |