forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor_loop.cpp
110 lines (98 loc) · 3.22 KB
/
for_loop.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
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
/*******************************************************************************
*
* Program: For Loop Examples
*
* Description: Demonstration of how to use for loops in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=8CdKMqW4LC4
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
// You can comment out the different examples below by adding and removing
// the /* ... */ comments, this will let you try out the different
// examples one at a time.
// A for loop is an alternative to using a whie loop. Here we use a while
// loop to output the integers from 1-5. We use a counter variable i that
// is first initialized before the loop runs, the counter variable is
// checked during the loop condition, and the counter variable is modified
// at the end of the loop. This is a very common pattern in programming
// loops. But if the while loop body is very large, we would need to scroll
// around in the source code file to figure out how the loop is working,
// as the initialization, condition, and updating of the counter variable
// would not all be visible at once. This information is important to
// understand how a loop is going to behave. A for loop gives us a construct
// that allows us to put all this important information on a single line.
/*
int i = 1;
while (i <= 5)
{
cout << "i: " << i << endl;
i++;
}
*/
// An equivalent for loop looks like this. The first statement 'int i = 1;'
// in the for ( ... ) brackers is the initialization statement, the
// condition is i <= 5, and the update statement is i++.
for (int i = 1; i <= 5; i++)
{
cout << "i: " << i << endl;
}
// The control-flow of a for-loop like the above is given below. The
// for loop has 4 parts:
//
// - initialization statement
// - condition
// - update statement
// - body
//
// And those are found in these places:
//
// for (init; condition; update)
// {
// body
// }
//
// First the initialization statement will execute, then the condition will
// be checked. If the condition is false the loop will stop, if the condition
// is true the loop will continue. Next the body will exectute, followed by
// the update statement. And then the loop will "loop back" to checking
// the condition again.
//
// init
// |
// condition <--
// | |
// body |
// | |
// update ------
//
// Notably just because a for loop is typically used as in the above example,
// we aren't required use it this way. We can leave out the initialization
// and upate statements, we can do things like increment the counter variable
// in the loop body, and all of this is allowed.
//
/*
int i = 1;
for ( ; i <= 5; )
{
cout << "i: " << i << endl;
i++;
}
}
*/
// We can also have for loops with more than one counter variable, we could
// update both variables in the update statement by using a , comma.
//
/*
for (int i = 0, j = 1; i <= 5 && j <= 20 ; i++, j += 5)
{
cout << "i: " << i << ", j: " << j << endl;
}
*/
return 0;
}