Intro_loops
Loops.cpp
Go to the documentation of this file.
1 // Loops.cpp
2 // 3 versions of a (for/while/do-while)
3 //
4 // g++ -std=c++11 Loops.cpp -o loop_demo
5 
6 #include <iostream>
7 using namespace std;
8 
9 int main()
10 {
11  int n{5}; // -std=c++11
12 
13 // --------------------------------------- Classical loop
14 // rejecting loop
15  {
16  cout << endl << "Classical loop by for" << endl;
17 
18  // (initialization; continuation?; manipulation)
19  for (int i = 0; i < n; ++i) {
20  cout << " " << i;
21  }
22  cout << endl;
23  }
24 // --------------------------------------- While
25 // rejecting loop
26  {
27  cout << endl << "Loop by while" << endl;
28 
29  int i = 0; // initialization
30  while ( i < n ) { // continuation?
31  cout << " " << i;
32  ++i; // manipulation
33  }
34  cout << endl;
35  }
36 // --------------------------------------- Do While
37 // non-rejecting loop
38  {
39  cout << endl << "Loop by do while" << endl;
40  cout << " wrong for n <= 0 !!" << endl;
41 
42  int i(0); // initialization
43  do {
44  cout << " " << i;
45  ++i; // manipulation
46  }
47  while ( i < n ); // continuation?
48 
49  cout << endl;
50  }
51  return 0;
52 }
53 
54 
55 
56 
57 
58 
59 
60 
61 
int main()
Definition: Loops.cpp:9