FOR Repetition Statement - C++ controlled loop

Concluding the presentation of the repetition structures of our C ++ Course, we will present the FOR statement, the controlled loop.

FOR repetition statement in C++

The FOR repetition statement has the following syntax:
for(inicialization ; conditional_test ; update){
   // codes that run if
   // the conditional test
   // is true
}
Come on.
The FOR repetition statement has three expressions within it, separated by semicolons.

The loop begins with some kind of initialization, usually a count variable, with some initial value.

After this initialization, conditional testing occurs. If true, the code inside the FOR loop braces is executed.
After each iteration, the 'update' occurs, where we usually update the counter value, most commonly it is a variable that will increment or decrement.

Then again the conditional test is performed, if true, again the FOR code is executed. After this iteration, the update occurs again.

FOR loop usage example

In programming, an example is worth a thousand words.
Let's count from 1 to 10, using the for loop, the code is as follows:
#include <iostream>
using namespace std;

int main()
{
    int count;

    for(count=1; count<=10 ; count++){
        cout << count << endl;
    }

    return 0;
}
Our control variable is count, which starts at 1.
Let's print it on screen as long as its value is less than or equal to 10.

At each iteration, we increment it by one (count++) because we want it to go from 1 to 10.

Note that we already did that in the WHILE loop, but we initialized the variables before the loop, inside that we updated the variable with each loop and the conditional test occurred inside the WHILE parentheses.

It occurs the same way in the FOR loop, but in a more organized way.

Using the FOR Repetition Statement

Let's do the opposite now, a countdown, which counts from 100 to 1.
For this, we initialize our control variable to 100.

The test to be performed is: count> 0
That is, as long as the variable has a value above 0, iterations of the FOR loop will occur.

And with each iteration we have to decrease the count, because it goes from 100 to 1, one by one.
See how our code looked:

#include <iostream>
using namespace std;

int main()
{
    int count;

    for(count=100; count>0 ; count--){
        cout << count << endl;
    }

    return 0;
}
Not always, however, we will increment or decrement by 1 in 1.
We can update our variables as we wish.

For example, let's print all even numbers from 1 to 1000 on the screen.
The first even is 2, so we initialize our variable with this value.

Let's increment by 2: count += 2

And the test is as long as the variable is less than or equal to one thousand: count <= 1000
#include <iostream>
using namespace std;

int main()
{
    int count;

    for(count=2; count<=1000 ; count+=2){
        cout << count << endl;
    }

    return 0;
} 
See the incredible speed with which this code executes.
Do you doubt the ability of C ++? Put 1 million.

When to use the FOR loop

Often we want to loop with a certain number of iterations.
For example, when asking for a student's grades, let's ask for as many grades as there are to average.

To calculate your income tax, we need to add up your entire salary of the year, ie 12 salaries.

The FOR loop is ideal when you know exactly the number of iterations you are going to do: "oh, I want to calculate this in this range from x to y", so bang, use the FOR repetition statement.

When you don't know when looping should end or have less control over how many iterations to occur, then use the WHILE loop.

Remembering that, deep down, they are absolutely the same thing.

It will only be easier to work with FOR sometimes and WHILE at other times.

FOR Repetition Statement

Create a program that asks how many grades you want to average, then asks each of those grades and finally displays the average.

Let's store the number of notes we will ask for in variable n.
The next step is to ask for grade by grade, and here comes the ace in the hole: calculate the sum of all grades.

Within the FOR, the control variable aux goes from grade 1 to the grade n, asking one by one and storing the grade in the variable grade.

Let's store in the sum variable, the sum of all these grades.
Finally, we display sum/n to display the average.

See our C ++ code:
#include <iostream>
using namespace std;

int main()
{
    int aux, n;
    float grade, sum=0;

    cout <<"How many topics: ";
    cin >> n;

    for(aux=1; aux<=n ; aux++){
        cout <<"Grade "<<aux<<": ";
        cin >> grade;
        sum += grade;
    }

    cout << "Average: "<<(sum/n)<<endl;

    return 0;
}
Notice how this repetition statement is controlled: it will always run 'n' iterations, whatever the value of 'n' (obviously, the number of grade must be a positive integer value).

You can fill in 2 grades, 3 grades, a thousand grades, a million grades ...
Powerful, isn't this FOR command?

No comments:

Post a Comment