-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.cpp
More file actions
53 lines (44 loc) · 1.11 KB
/
loops.cpp
File metadata and controls
53 lines (44 loc) · 1.11 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
#include "Learn_CPP.h"
// Function to initialize loop topics
std::unordered_map<std::string, Topic> getLoopTopics() {
std::unordered_map<std::string, Topic> topics;
topics["Loops"] = {
"Loops are used to execute a block of code repeatedly, either a fixed number of times or until a condition is met. Common types of loops are for, while, and do-while loops.",
R"(Syntax:
1. For loop:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
2. While loop:
while (condition) {
// Code to be executed
}
3. Do-While loop:
do {
// Code to be executed
} while (condition);)",
R"(Example Code:
#include <iostream>
using namespace std;
int main() {
// For loop
for (int i = 0; i < 5; i++) {
cout << "For Loop Iteration: " << i << endl;
}
// While loop
int x = 0;
while (x < 3) {
cout << "While Loop Iteration: " << x << endl;
x++;
}
// Do-While loop
int y = 0;
do {
cout << "Do-While Loop Iteration: " << y << endl;
y++;
} while (y < 2);
return 0;
})"
};
return topics;
}