-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (63 loc) · 2.35 KB
/
main.cpp
File metadata and controls
72 lines (63 loc) · 2.35 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
#include <iostream>
int main() {
std::cout << "--- 01_02_Flow: Control Flow Structures ---" << std::endl;
// 1. If/Else
// Used to execute code blocks based on boolean conditions.
std::cout << "\n[1] If/Else Statements" << std::endl;
int temperature = 25;
std::cout << "Current temperature: " << temperature << "C" << std::endl;
if (temperature > 30) {
std::cout << "It is hot outside." << std::endl;
} else if (temperature > 20) {
std::cout << "It is a pleasant day." << std::endl;
} else {
std::cout << "It is cold." << std::endl;
}
// 2. While Loop
// Repeats a block of code as long as a condition is true.
std::cout << "\n[2] While Loop" << std::endl;
int fuel = 5;
std::cout << "Starting engine with fuel: " << fuel << std::endl;
while (fuel > 0) {
std::cout << "Vroom... (Fuel: " << fuel << ")" << std::endl;
fuel--;
}
std::cout << "Engine stopped (Out of fuel)." << std::endl;
// 3. Do/While Loop
// Similar to while, but guarantees the code block runs at least once.
std::cout << "\n[3] Do/While Loop" << std::endl;
int retry_count = 0;
do {
std::cout << "Connecting to server... (Attempt " << retry_count + 1 << ")" << std::endl;
retry_count++;
} while (retry_count < 3);
std::cout << "Connection failed after 3 attempts." << std::endl;
// 4. For Loop
// Ideal for iterating a specific number of times or over arrays.
std::cout << "\n[4] For Loop" << std::endl;
std::cout << "Counting even numbers:" << std::endl;
for (int i = 0; i <= 10; i += 2) {
std::cout << i << " ";
}
std::cout << std::endl;
// 5. Switch Statement
// A cleaner way to select between many values for a single variable.
std::cout << "\n[5] Switch Statement" << std::endl;
char command = 'S'; // 'S' for Start
std::cout << "Received command: " << command << std::endl;
switch (command) {
case 'S':
std::cout << "Action: Start System" << std::endl;
break;
case 'P':
std::cout << "Action: Pause System" << std::endl;
break;
case 'Q':
std::cout << "Action: Quit" << std::endl;
break;
default:
std::cout << "Action: Unknown Command" << std::endl;
break;
}
return 0;
}