For Loop statement
Executes the statement repeatedly until the condition becomes false.
Grammar
for (init-expression; cond-expression; loop-expression)
statement;
Note
Use a For statement to build a loop that must perform a specified number of times.
The For statement includes three optional parts, as shown in the following table.
For loop element
The following example displays different methods for using a for statement.
#include <iostream>
using namespace std;
int main () {
//The counter variable can be declared in the init-expression.
for (int i = 0; i < 2; i++) {
cout << i
}
OUTPUT:01
//The counter variable can is declared outside the for loop.
int i;
for (i = 0; i < 2; i++) {
cout << i
}
OUTPUT:01
//loops are the equivalent of a while loop.
i = 0;
while (I < 2) {
cout << i++;
}
}
output:012
init-expression and loop-expression can contain multiple statements separated by commas. For example:
#include <iostream>
using namespace std;
int main () {
int i, J;
for (i = 5, j = i + J < i++, J + +) {
cout << "i + j =" << (i + j) << ' \ n ';
}
}
Output:
i + j = i
+ j =
i + j = 19
Loop-expression can be incremented or decremented, or modified by other means.
#include <iostream>
using namespace std;
int main () {for
(int i = i > 0; i--) {
cout << i << ';
}
Output:10 9 8 7 6 5 4 3 2 1 for
(int i = i < i = i+2) {
cout << i << ';
}
Output:10 12 14 16 18
The For loop terminates when a break, return, or goto in statement is executed (to a tagged statement outside the For loop). The continue statement in the For loop terminates only the current iteration.
If Cond-expression is omitted, the True,for loop is considered to be not terminated when there is no break, return, or goto in statement.
Although the three fields of a for statement are typically used for initialization, test termination conditions, and increments, they are not limited to these purposes. For example, the following code prints the numbers 0 through 4. In this case, the statement is a NULL statement:
#include <iostream>
using namespace std;
int main ()
{
int i;
for (i = 0; i < 5; cout << i << ' \ n ', i++) {
;
}
}
For loop and C + + standards
The C + + standard mentions that the variable declared in the For loop will go out of scope after the for loop ends. For example:
for (int i = 0; i < 5, i++) {
//do something
}
//I are now out of scope under/za Or/zc:forscope
By default, variables declared in the For loop remain in scope until the closed scope of the for loop terminates, under/ze.
/zc:forscope the standard behavior of variables declared in a For loop can be enabled without specifying/za.
You can also use the range variance for the For loop to/ze the variables under the, as follows:
For_statement5.cpp
int Main () {
int i = 0; Hidden by Var and same name declared in for loop for
(int i = 0; i < 3; i++) {} for
(int i = 0; ; 3; i++) {}
}
This is more like the standard behavior of a variable declared in a for loop, which requires the variable declared in the For loop to be out of range after the loop has finished. After declaring a variable in the for loop, the compiler promotes it internally to a local variable in the enclosing scope of the For loop, even if there is a local variable with the same name.
Based on scope for statement
Statement statement executes each element in the statement expression sequentially.
Grammar
for (for-range-declaration:expression)
statement
Notes
Constructs a loop scope that must be performed by using a scoped for statement, which can be defined as either a circular access, such as std::vector, or any other scope defined with the Begin () and end (). The name in the For-range-declaration statement is for, and cannot be declared again in expression or statement. Note that the Automatic keyword is preferred for partial statements in For-range-declaration.
This code shows how to use a for-scope loop to traverse an array and vector:
Range-based-for.cpp//Compile by using:cl/ehsc/nologo/w4 #include <iostream> #include <vector> using
namespace Std;
int main () {//Basic 10-element integer array.
int x[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Range-based for loop to iterate through the array.
for (int y:x) {//Access by value using a copy declared as specific type.
Not preferred.
cout << y << "";
} cout << Endl; The Auto keyword causes type inference to is used.
Preferred.
for (auto y:x) {//Copy of ' X ', almost always undesirable cout << y << "";
} cout << Endl;
for (auto &y:x) {//Type inference by reference. observes and/or modifies in-place.
Preferred when modify is needed.
cout << y << "";
} cout << Endl;
for (const auto &Y:X) {//Type inference by reference. Observes In-place.
Preferred when no modify is needed. cout <<
Y << "";
} cout << Endl;
cout << "End of integer array test" << Endl;
cout << Endl;
Create a vector object that contains ten elements.
Vector<double> v;
for (int i = 0; i < ++i) {V.push_back (i + 0.14159);
}//range-based for loop to iterate through the vector, observing in-place.
for (const auto &j:v) {cout << J << "";
} cout << Endl;
cout << "End of vector test" << Endl;
}
The output is as follows:
The 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 end of the
integer array t EST
0.14159 1.14159 2.14159 3.14159 4.14159 5.14159 6.14159 7.14159 8.14159 9.14159 end of
vector test
A For loop terminates at statement execution: Break, return, or goto A for loop outside a statement continue the iteration with the statement to terminate the current for loop.
Remember these facts about the scope for
automatically recognizes arrays.
Identify the containers that have. Begin () and. End ().
Use the lookup begin () and end () based on the argument.