Presentation is loading. Please wait.

Presentation is loading. Please wait.

Boolean Variables & Values

Similar presentations


Presentation on theme: "Boolean Variables & Values"— Presentation transcript:

1 Boolean Variables & Values

2 Motivation: why Booleans?
1. To "save and reuse" the result of a "complex" logical decision 2. To improve Clarity of the source code 3. To improve the Efficiency of decisions in a program.

3 bool is a C++ Data Type Boolean literals: true false Example declarations: bool isVIP = true; bool error = false; const bool verdad = true;

4 Assignment Syntax: boolVar = conditional expression ; Example declarations: bool isPos; int a = 3; isPos = (a > 0); // isPos=true

5 Example Engineering Honor Student: must have 1. completed 48 credits overall 2. completed 24 credits in EGR 3. have a GPA of 3.25 or higher overall 4. have a GPA of 3.5 or higher in EGR courses.

6 Example bool isEgrHonor; isEgrHonor = (totCredits >= 48) && (egrCredits >= 24) && (gpa >= 3.0) && (egrGpa >= 3.5); ... if (isEgrHonor) cout << "Engineering Honor Student";

7 Do not cin/cout Booleans They will not work as expected.
User I/O Do not cin/cout Booleans They will not work as expected.

8 User I/O cin >> yesNo; isEgrHonor = (yesNo == "Yes"); ...
bool isEgrHonor; string yesNo; cout << "Is EGR Honor Student? (Yes/No)"; cin >> yesNo; isEgrHonor = (yesNo == "Yes"); ... if (isEgrHonor) cout << "Is an EGR Honor Student"; else cout << "Is NOT an EGR Honor Stud";

9 Vocabulary Term Definition Boolean
a value of true or false; a variable with a value of true or fals


Download ppt "Boolean Variables & Values"

Similar presentations


Ads by Google