-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.cpp
More file actions
34 lines (30 loc) · 914 Bytes
/
loops.cpp
File metadata and controls
34 lines (30 loc) · 914 Bytes
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
#include <iostream>
using namespace std;
int main() {
// -------- FOR LOOP --------
// Syntax: for(initialization; condition; update)
// Best when we know how many times to repeat
cout << "For loop example:\n";
for (int i = 1; i <= 5; i++) {
cout << "i = " << i;
}
// -------- WHILE LOOP --------
// Syntax: while(condition) { ... }
// Best when number of repetitions is unknown; checks condition before running
cout << "\nWhile loop example:\n";
int j = 1;
while (j <= 5) {
cout << "j = " << j;
j++;
}
// -------- DO-WHILE LOOP --------
// Syntax: do { ... } while(condition);
// Runs the block at least once, checks condition after running
cout << "\nDo-while loop example:\n";
int k = 1;
do {
cout << "k = " << k;
k++;
} while (k <= 5);
return 0;
}