-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif-else.cpp
More file actions
106 lines (89 loc) · 3.63 KB
/
Copy pathif-else.cpp
File metadata and controls
106 lines (89 loc) · 3.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using namespace std;
int main() {
// =========================================================================
// 1. COMPARISON OPERATORS (Used to compare two values)
// =========================================================================
// < : Less than
// <= : Less than or equal to
// > : Greater than
// >= : Greater than or equal to
// == : Equal to
// != : Not equal to
int time = 20;
// =========================================================================
// 2. BASIC IF...ELSE STATEMENT
// =========================================================================
// Checks a single condition. Runs 'if' block if true, otherwise 'else' block.
if (time < 18) {
cout << "Good day." << endl;
} else {
cout << "Good evening." << endl; // Output: Good evening.
}
// =========================================================================
// 3. USING BOOLEAN VARIABLES IN CONDITIONS
// =========================================================================
// You can store conditions inside boolean variables (true/false) for clean code.
bool isDay = time < 18; // Stores 'false' because 20 is not less than 18
if (isDay) {
cout << "Good day." << endl;
} else {
cout << "Good evening." << endl;
}
// =========================================================================
// 4. IF...ELSE IF...ELSE LADDER
// =========================================================================
// Checks multiple conditions sequentially from top to bottom.
int temp = 30;
if (temp < 0) {
cout << "It's freezing!\n";
} else if (temp < 20) {
cout << "It's cool.\n";
} else {
cout << "It's warm.\n"; // Output: It's warm.
}
// =========================================================================
// 5. LOGICAL OPERATORS (AND, OR, NOT)
// =========================================================================
int a = 200, b = 33, c = 500;
// AND (&&) : Returns true ONLY IF ALL conditions are true
if (a > b && c > a) {
cout << "Both conditions are true!\n";
}
// OR (||) : Returns true IF AT LEAST ONE condition is true
if (a > b || a > c) {
cout << "At least one condition is true!\n";
}
// NOT (!) : Reverses the boolean result (true -> false, false -> true)
if (!(a < b)) {
cout << "a is NOT less than b!\n";
}
// =========================================================================
// 6. NESTED IF STATEMENTS
// =========================================================================
// Placing an 'if' statement inside another 'if' statement for secondary checks.
int age = 20;
bool isCitizen = true;
if (age >= 18) {
cout << "Old enough to vote.\n";
// Inner check: executed only if the outer condition (age >= 18) is true
if (isCitizen) {
cout << "You are eligible to cast your vote.\n";
} else {
cout << "You must be a citizen to vote.\n";
}
} else {
cout << "Not old enough to vote.\n";
}
// =========================================================================
// 7. PRACTICAL EXAMPLE: CHECK EVEN OR ODD NUMBER
// =========================================================================
// The modulus operator (%) returns the remainder of division.
int myNum = 5;
if (myNum % 2 == 0) {
cout << myNum << " is even.\n";
} else {
cout << myNum << " is odd.\n"; // Output: 5 is odd.
}
return 0;
}