C++ Tutorial/Data Types/bool — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:31, 25 мая 2010
Содержание
- 1 A bool value can control the if statement
- 2 bool values
- 3 Logical operators.
- 4 Outcome of a relational operator is a true/false value
- 5 Output boolean variable as boolean literal or number
- 6 The !(Exclamation mark) Operator
- 7 The || Operator
- 8 The && Operator (amplifier)
- 9 Use bool value to control a while loop
- 10 Variable size of bool
A bool value can control the if statement
<source lang="cpp">#include <iostream> using namespace std;
int main() {
bool b; b = false; cout << "b is " << b << "\n"; b = true; cout << "b is " << b << "\n"; if(b) cout << "This is executed.\n"; b = false; if(b) cout << "This is not executed.\n"; return 0;
}</source>
b is 0 b is 1 This is executed.
bool values
<source lang="cpp">#include <iostream> using namespace std;
int main() {
bool b; b = false; cout << "b is " << b << "\n"; b = true; cout << "b is " << b << "\n"; if(b) cout << "This is executed.\n"; b = false; if(b) cout << "This is not executed.\n"; return 0;
}</source>
b is 0 b is 1 This is executed. 10 > 9 is 1
Logical operators.
<source lang="cpp">#include <iostream> using std::cout; using std::endl; using std::boolalpha; // causes bool values to print as "true" or "false" int main() {
// create truth table for && (logical AND) operator cout << boolalpha << "Logical AND (&&)" << "\nfalse && false: " << ( false && false ) << "\nfalse && true: " << ( false && true ) << "\ntrue && false: " << ( true && false ) << "\ntrue && true: " << ( true && true ) << "\n\n"; // create truth table for || (logical OR) operator cout << "Logical OR (||)" << "\nfalse || false: " << ( false || false ) << "\nfalse || true: " << ( false || true ) << "\ntrue || false: " << ( true || false ) << "\ntrue || true: " << ( true || true ) << "\n\n"; // create truth table for ! (logical negation) operator cout << "Logical NOT (!)" << "\n!false: " << ( !false ) << "\n!true: " << ( !true ) << endl; return 0;
}</source>
Logical AND (&&) false && false: false false && true: false true && false: false true && true: true Logical OR (||) false || false: false false || true: true true || false: true true || true: true Logical NOT (!) !false: true !true: false
Outcome of a relational operator is a true/false value
<source lang="cpp">#include <iostream> using namespace std;
int main() {
bool b; b = false; cout << "b is " << b << "\n"; b = true; cout << "b is " << b << "\n"; return 0;
}</source>
b is 0 b is 1 10 > 9 is 1
Output boolean variable as boolean literal or number
<source lang="cpp">#include <iostream>
- include <iomanip>
using namespace std; int main(int argc, char** argv) {
bool myBool = true; cout << "This is the default: " << myBool << endl; cout << "This should be true: " << boolalpha << myBool << endl; cout << "This should be 1: " << noboolalpha << myBool << endl;
}</source>
The !(Exclamation mark) Operator
<source lang="cpp">#include <iostream> using namespace std; int main(void) {
int age; cout << "Enter your age: "; cin >> age; if (!(age > 12 && age < 65)) cout << "Admission is free"; else cout << "You have to pay"; return 0;
}</source>
The || Operator
<source lang="cpp">#include <iostream> using namespace std; int main(void) {
int age; cout << "Enter your age: "; cin >> age; if (age <= 12 || age >= 65) cout << "Admission is free"; else cout << "You have to pay"; return 0;
}</source>
The && Operator (amplifier)
<source lang="cpp">#include <iostream> using namespace std; int main(void) {
int age; char choice; bool citizen; cout << "Enter your age: "; cin >> age; cout << "Are you a citizen (Y/N): "; cin >> choice; if (choice == "Y") citizen = true; else citizen = false; if (age >= 18 && citizen == true) cout << "You are eligible to vote"; else cout << "You are not eligible to vote"; return 0;
}</source>
Use bool value to control a while loop
<source lang="cpp">#include <iostream>
- include <string>
using namespace std; int main() {
string best_name = ""; double best_price = 1; int best_score = 0; bool more = true; while (more) { string next_name; double next_price; int next_score; cout << "Please enter the model name: "; getline(cin, next_name); cout << "Please enter the price: "; cin >> next_price; cout << "Please enter the score: "; cin >> next_score; string remainder; /* read remainder of line */ getline(cin, remainder); if (next_score / next_price > best_score / best_price) { best_name = next_name; best_score = next_score; best_price = next_price; } cout << "More data? (y/n) "; string answer; getline(cin, answer); if (answer != "y") more = false; } cout << "The best value is " << best_name << " Price: " << best_price << " Score: " << best_score << "\n"; return 0;
}</source>
Variable size of bool
<source lang="cpp">#include <iostream>
int main() {
using std::cout; cout << "The size of a bool is:" << sizeof(bool) << " bytes.\n"; return 0;
}</source>
The size of a bool is:1 bytes.