-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathloops.cpp
More file actions
59 lines (48 loc) · 1.12 KB
/
loops.cpp
File metadata and controls
59 lines (48 loc) · 1.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
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
//for loop.
cout << "\n\tFOR LOOP\n";
int forSum = 0;
for (int i = 0; i < 10; ++i){
cout << i << "th iteration.\n";
forSum = forSum + i;
if (forSum < 25) {
continue;
//continue statement is used to skip next lines and retest loop condition.
}
else {
cout << "Sum is " << forSum << endl << endl;
}
}
cout << "Sum is " << forSum << endl << endl;
//While Loop
//while loop is a entry controlled loop
cout << "\n\tWHILE LOOP\n";
int myCounter = 10;
int whileSum = 0;
while (myCounter > 0) {
cout << "Value of counter is " << myCounter << endl;
whileSum = whileSum + myCounter;
myCounter--;
cout << "Sum is " << whileSum << endl << endl;
}
cout<< "Sum is "<< whileSum << endl;
//do-while loop is a post test loop. It is always executed at least once.
cout << "\n\tDO-WHILE LOOP\n";
int doWhile = 3;
do {
cout << "Value of counter is " << doWhile << endl;
doWhile--;
} while(doWhile!=0);
//infite loops : execute for infinite times.
/*
for ( ; ; ){
Code here.
}
while(1){
Code here.
}
*/
return 0;
}