-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp1_5.html
More file actions
91 lines (81 loc) · 3.36 KB
/
Copy pathCpp1_5.html
File metadata and controls
91 lines (81 loc) · 3.36 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
<article>
<h1>C++ I: Page 5 - Loops: For & While</h1>
<section>
<h2>Automating Tasks with Repetition</h2>
<p>Loops allow you to repeat actions. C++ supports <code>for</code>, <code>while</code>, <code>do-while</code>, and the modern range-based <code>for</code> loop (C++11 and later).</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: loops.cpp
* Demonstration of various C++ looping mechanisms with control flow
*/
#include <iostream>
#include <vector>
using namespace std;
int main() {
// 1. Traditional For Loop
cout << "For loop (counting):" << endl;
for (int i = 0; i < 3; i++) {
cout << " Iteration " << i << endl;
}
// 2. While Loop
cout << "While loop (condition-based):" << endl;
int j = 0;
while (j < 3) {
cout << " Iteration " << j << endl;
j++;
}
// 3. Do-While Loop (Always runs at least once)
cout << "Do-While loop:" << endl;
int k = 0;
do {
cout << " Iteration " << k << endl;
k++;
} while (k < 0); // Still executes once
// 4. Range-based For Loop (C++11)
vector<int> nums = {10, 20, 30};
cout << "Range-based loop:" << endl;
for (int n : nums) {
cout << " Value: " << n << endl;
}
// 5. Advanced Loop Control: break/continue
cout << "Loop control example:" << endl;
for (int i = 0; i < 10; ++i) {
if (i == 2) continue; // Skip 2
if (i == 5) break; // Exit at 5
cout << " Value: " << i << endl;
}
return 0;
}
/*
* Loop Nuances:
* 1. 'break' exits the loop immediately.
* 2. 'continue' skips the rest of the current iteration and jumps to the next.
* 3. Range-based loops are safer and more concise for containers,
* preventing off-by-one errors.
* 4. Infinite loops (e.g., while(true)) require a 'break' condition.
* 5. Do-while guarantees at least one execution, useful for input validation.
* 6. Always initialize your loop counter. Uninitialized variables lead to
* undefined behavior.
* 7. When nested loops occur, 'break' only exits the innermost loop.
*/
</code></pre>
<div class="interactive-sim">
<h3>Loop Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp1_5');
out.innerHTML = 'for (int i=0; i<3; i++) {<br>';
setTimeout(() => out.innerHTML += '>> Iteration ' + 0 + '<br>', 400);
setTimeout(() => out.innerHTML += '>> Iteration ' + 1 + '<br>', 800);
setTimeout(() => out.innerHTML += '>> Iteration ' + 2 + '<br>', 1200);
setTimeout(() => out.innerHTML += '}<br>', 1600);
">Run Simulation</button>
<div id="sim-output-cpp1_5" 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_4.html" style="text-decoration: none; color: #6c757d;">← Previous: Control Flow: Conditionals</a>
<a href="#" data-file="Cpp1_6.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: Memory: Stack vs Heap →</a>
</div>
</footer>
</article>