-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.cpp
More file actions
69 lines (51 loc) · 2.22 KB
/
Copy pathboolean.cpp
File metadata and controls
69 lines (51 loc) · 2.22 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
using namespace std;
int main() {
// ==========================================
// 1. BOOLEAN VALUES & DATA TYPE
// ==========================================
cout << "=== 1. BOOLEAN VALUES ===" << endl;
// 'bool' can only hold true (1) or false (0)
bool isCodingFun = true;
bool isFishTasty = false;
// By default, C++ outputs 1 for true and 0 for false
cout << "isCodingFun : " << isCodingFun << endl; // Output: 1
cout << "isFishTasty : " << isFishTasty << endl; // Output: 0
// ==========================================
// 2. PRINTING true/false WITH boolalpha
// ==========================================
cout << "\n=== 2. USING boolalpha ===" << endl;
// Use 'boolalpha' to print "true" or "false" instead of 1 or 0
cout << boolalpha;
cout << "isCodingFun : " << isCodingFun << endl; // Output: true
cout << "isFishTasty : " << isFishTasty << endl; // Output: false
// ==========================================
// 3. BOOLEAN EXPRESSIONS (Comparisons)
// ==========================================
cout << "\n=== 3. BOOLEAN EXPRESSIONS ===" << endl;
int x = 10;
int y = 9;
// Comparing values returns true (1) or false (0)
cout << "Is x > y? : " << (x > y) << endl; // Output: true
cout << "Is 10 == 15?: " << (10 == 15) << endl; // Output: false
// ==========================================
// 4. STORING RESULT IN A BOOL VARIABLE
// ==========================================
cout << "\n=== 4. STORE RESULT IN VARIABLE ===" << endl;
// You can store a comparison result directly inside a bool variable
bool isGreater = (x > y);
cout << "isGreater : " << isGreater << endl; // Output: true
// ==========================================
// 5. REAL LIFE EXAMPLE (Decision Making)
// ==========================================
cout << "\n=== 5. REAL LIFE EXAMPLE ===" << endl;
int myAge = 25;
int votingAge = 18;
// Check if eligible to vote using >= (greater than or equal to)
if (myAge >= votingAge) {
cout << "Old enough to vote!" << endl;
} else {
cout << "Not old enough to vote." << endl;
}
return 0;
}