C++ for loop
In C++, the for loop is a control structure used to execute a block of code repeatedly for a fixed number of times or until a certain condition is met.
It is a compact and powerful loop construct that can be used for a wide range of applications.
The syntax for a for loop in C++ is as follows:
css code
for (initialization;
condition;
increment)
{
//code to be executed repeatedly
}
The initialization statement is executed only once, at the beginning of the loop, and it sets the initial value of the loop control variable.
The condition statement is evaluated before each iteration of the loop, and if it is true, the loop body is executed.
The increment statement is executed at the end of each iteration, before the condition is checked again.
Here is an example of a simple for loop that counts from 0 to 9:
cssCopy code
for (int i = 0; i < 10; i++) { cout << i << endl; }
In this example, the initialization statement initializes the loop control variable i to 0. The condition statement checks whether i is less than 10, and if it is true, the loop body is executed.
The increment statement increments i by 1 at the end of each iteration.
The output of this program would be:
Copy code
0 1 2 3 4 5 6 7 8 9
The for loop is often used to iterate over arrays or other data structures.
For example, the following code uses a for loop to calculate the sum of the elements of an array:
cCopy code
int array[] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0;
i < 5;
i++) { sum += array[i];
} cout << "The sum is " << sum << endl;
In this example, the loop control variable i is used as an index to access each element of the array in turn.
The loop continues until i reaches the end of the array (which has 5 elements).
The for loop can also be used with other control structures, such as if statements or switch statements.
For example, the following code uses a for loop and an if statement to print out all the even numbers from 0 to 10:
css code
for (int i = 0;
i <= 10;
i++)
{
if (i % 2 == 0)
{ cout << i << endl;
}
In this example, the condition statement checks whether i is even (i.e., whether it is divisible by 2), and if it is, the code inside the if statement is executed.
The for loop can also be used to create infinite loops, which continue until a break statement is encountered.
For example, the following code uses a for loop and a break statement to repeatedly prompt the user for input until a valid integer is entered:
css code
for (;;)
{ cout << "Enter an integer: ";
int n;
cin >> n;
if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n');
} else { cout << "You entered " << n << endl; break; } }
In this example, the for loop has no initialization statement, no condition statement, and no increment statement, so it creates an infinite loop.
The code inside the loop prompts the user for input and checks whether the input is a valid integer.
If it is not, the input stream is cleared and the input is discarded. If it is, the input is printed
0 Comments