generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_FlowControl_For.cpp
44 lines (38 loc) · 1.4 KB
/
04_FlowControl_For.cpp
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
#include <iostream>
#include <vector>
int main()
{
std::cout << "1) typical loop with a single statement as the body:\n";
for (int i = 0; i < 10; ++i)
std::cout << i << ' ';
std::cout << "\n\n" "2) init-statement can declare multiple names, as\n"
"long as they can use the same decl-specifier-seq:\n";
for (int i = 0, *p = &i; i < 9; i += 2)
std::cout << i << ':' << *p << ' ';
std::cout << "\n\n" "3) condition may be a declaration:\n";
char cstr[] = "Hello";
for (int n = 0; char c = cstr[n]; ++n)
std::cout << c;
std::cout << "\n\n" "4) init-statement can use the auto type specifier:\n";
std::vector<int> v = {3, 1, 4, 1, 5, 9};
for (auto iter = v.begin(); iter != v.end(); ++iter)
std::cout << *iter << ' ';
std::cout << "\n\n" "5) init-statement can be an expression:\n";
int n = 0;
for (std::cout << "Loop start\n";
std::cout << "Loop test\n";
std::cout << "Iteration " << ++n << '\n')
{
if (n > 1)
break;
}
std::cout << "\n" "6) constructors and destructors of objects created\n"
"in the loop's body are called per each iteration:\n";
struct S
{
S(int x, int y) { std::cout << "S::S(" << x << ", " << y << "); "; }
~S() { std::cout << "S::~S()\n"; }
};
for (int i{0}, j{5}; i < j; ++i, --j)
S s{i, j};
}