-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor-loop
More file actions
116 lines (86 loc) · 3.12 KB
/
Copy pathfor-loop
File metadata and controls
116 lines (86 loc) · 3.12 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
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
using namespace std;
int main() {
// ===================================================
// 1. BASIC FOR LOOP
// ===================================================
// Syntax: for (statement 1; statement 2; statement 3)
// Statement 1: Executed once before loop starts (init)
// Statement 2: Condition for executing the loop
// Statement 3: Executed every time after the loop block runs
cout << "=== 1. BASIC FOR LOOP (0 TO 4) ===" << endl;
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
// ===================================================
// 2. COUNTDOWN & STEP INCREMENTS
// ===================================================
cout << "\n=== 2. COUNTDOWN (5 TO 1) ===" << endl;
for (int i = 5; i > 0; i--) {
cout << i << "\n";
}
cout << "\n=== STEP BY 10 (0 TO 100) ===" << endl;
for (int i = 0; i <= 100; i += 10) {
cout << i << "\n";
}
cout << "\n=== EVEN NUMBERS (0 TO 10) ===" << endl;
for (int i = 0; i <= 10; i = i + 2) {
cout << i << "\n";
}
cout << "\n=== ODD NUMBERS (1 TO 10) ===" << endl;
for (int i = 1; i <= 10; i = i + 2) {
cout << i << "\n";
}
cout << "\n=== POWERS OF 2 (UP TO 512) ===" << endl;
for (int i = 2; i <= 512; i *= 2) {
cout << i << "\n";
}
// ===================================================
// 3. SUM OF NUMBERS & MULTIPLICATION TABLE
// ===================================================
cout << "\n=== 3. SUM OF NUMBERS (1 TO 5) ===" << endl;
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
cout << "Sum is " << sum << endl;
cout << "\n=== MULTIPLICATION TABLE FOR 2 ===" << endl;
int number = 2;
for (int i = 1; i <= 10; i++) {
cout << number << " x " << i << " = " << number * i << "\n";
}
// ===================================================
// 4. NESTED LOOPS
// ===================================================
// Loop inside another loop (inner loop runs for each outer iteration)
cout << "\n=== 4. NESTED LOOPS ===" << endl;
for (int i = 1; i <= 2; ++i) {
cout << "Outer: " << i << "\n"; // Executes 2 times
for (int j = 1; j <= 3; ++j) {
cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
}
}
cout << "\n=== MULTIPLICATION TABLE GRID (1 TO 3) ===" << endl;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << i * j << " ";
}
cout << "\n";
}
// ===================================================
// 5. THE FOR-EACH LOOP (C++11 RANGE-BASED)
// ===================================================
// Syntax: for (type variableName : arrayName)
cout << "\n=== 5. FOR-EACH LOOP WITH ARRAY ===" << endl;
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int num : myNumbers) {
cout << num << "\n";
}
cout << "\n=== FOR-EACH LOOP WITH STRING ===" << endl;
string word = "Hello";
for (char c : word) {
cout << c << "\n";
}
return 0;
}