-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_4.html
More file actions
98 lines (88 loc) · 3.57 KB
/
Copy pathCpp1_4.html
File metadata and controls
98 lines (88 loc) · 3.57 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
<article>
<h1>C++ I: Page 4 - Control Flow: Conditionals</h1>
<section>
<h2>Making Decisions</h2>
<p>C++ uses <code>if</code>, <code>else if</code>, <code>else</code>, and <code>switch</code> to control the flow of execution based on conditions. Understanding conditional branching is the foundation of creating dynamic and reactive programs.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: control_flow.cpp
* Demonstration of if-else and switch control structures with complex logic
*/
#include <iostream>
#include <string>
using namespace std;
int main() {
int score = 85;
bool hasExtraCredit = true;
// 1. Complex If-Else-If Structure
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
// Nested condition
if (hasExtraCredit) {
cout << "Grade: A-" << endl;
} else {
cout << "Grade: B" << endl;
}
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: F" << endl;
}
// 2. Advanced Switch Statement
int day = 3; // Wednesday
switch (day) {
case 1: cout << "Monday" << endl; break;
case 2: cout << "Tuesday" << endl; break;
case 3:
case 4:
case 5:
cout << "Mid-week (Wed/Thu/Fri)" << endl;
break; // Fall-through used effectively here
default: cout << "Weekend" << endl;
}
// 3. Logical combinations
bool isStudent = true;
bool hasID = false;
// Short-circuit evaluation demo
if (isStudent && (hasID || score > 80)) {
cout << "Access Granted." << endl;
} else {
cout << "Access Denied." << endl;
}
return 0;
}
/*
* Control Flow Nuances:
* 1. Switch cases require a 'break' to prevent fall-through.
* 2. Fall-through (omitting break) can be useful for grouping cases.
* 3. 'default' in switch acts as the final 'else'.
* 4. Logical && and || use "short-circuit" evaluation:
* - In (A && B), if A is false, B is NOT evaluated.
* - In (A || B), if A is true, B is NOT evaluated.
* 5. This short-circuiting can be used for performance and safety
* (e.g., checking for null pointers before dereferencing).
* 6. Nested 'if' statements are readable up to a point; beyond that,
* consider refactoring into small helper functions.
* 7. Ternary operator (?:) is concise for simple assignments but should
* not be nested as it becomes hard to read.
*/
</code></pre>
<div class="interactive-sim">
<h3>Switch Statement Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_4');
out.innerHTML = 'day = 3;<br>';
setTimeout(() => out.innerHTML += 'switch(day) { ... }<br>', 800);
setTimeout(() => out.innerHTML += '>> Wednesday', 1600);
">Run Simulation</button>
<div id="sim-output-cpp1_4" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_3.html" style="text-decoration: none; color: #6c757d;">← Previous: Operators & Logic</a>
<a href="#" data-file="Cpp1_5.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Loops: For & While →</a>
</div>
</footer>
</article>