(C++) Loops. Loop with parameter (for)


Imagine a situation where we need to display the same word on the screen, say the word "HELLO", 10 times. What do we do?
You can take and write a command 10 times cout << "HELLO";

But what if, it’s necessary not 10 times, but 20, 30, 40 times ?, and if 200 times? In this case, copying will take a very long time. And if it is necessary for the user to choose how many times he should display information on the screen?

To cope with the challenge, a special statement called LOOP

Loop - it is an algorithmic construction in which a certain sequence of commands is repeated several times.


In the C++ programming language, there are two types of loops: a loop with a variable (for) and a loop with a condition (while and do ... while)

We begin our acquaintance with the loops from the first view.

A LOOP WITH A VARIABLE OR WITH A KNOWN NUMBER OF STEPS (FOR)

It often happens that we know the number of repetitions of any action, or we can calculate the number of repetitions using the data we know. Some programming languages have a command like REPEAT (number of times) - that is, we can specify the exact number of repetitions.

It is interesting to trace how this loop works on a machine level:
1. a specific memory cell is allocated in memory and the number of repetitions is recorded in it,
2. When the program executes the loop body once, the contents of this cell (counter) are reduced by one.
3. The execution of the cycle ends when there is zero in this cell.

In the C ++ programming language, there is no such construct, but there is a for construct.

The general form of the for loop statement is as follows:
for (/*part 1*/; /*part 2*/; /*part 3*/ )
{
      /* one or more statements - body of loop*/;
}
This statement requires us to
1. explicitly allocated a memory cell, which will be a counter, and set its initial value
2. write a condition under which the body of the cycle will be executed
3. indicated how it will change the value in this cell.

In the practical part, we will try to display the word Hello 10 times. And in further tasks we will analyze this construction in more detail.

The for loop is a step-by-step means of performing repetitive actions. Let's take a closer look at how it works.

Typically, parts of a for loop perform the following steps:
1. Setting the initial value.
2. Performing a condition check to continue the loop.
3. Performing a loop action.
4. Updating the value (s) used in the test condition.
and then steps 2-4 are repeated until the condition is satisfied. As soon as the condition becomes false, the loop stops its operation and the statement following the for loop operator is executed.

Let us return to the general form of writing the loop operator and analyze in more detail all parts
for (/*part 1*/; /*part 2*/; /*part 3*/ )
{
      /* body of loop*/;
}

Part 1

responsible for setting the initial value of the loop variable (counter), ends with a semicolon
For example :
1) i=0; //assign the initial value equal to zero to the loop variable i. With such a record, the variable i must be declared before the loop

2) int i=0; //i can be declared immediately in the loop header, but
// in this case, after the loop runs, it will be erased from memory

3) ;  // there is no initialization and declaration of the loop variable in general,
// in this case, it can be declared before the loop
Part 2
this is a condition for continuing the for loop, it is checked for truth.

i<=10  // the loop will run as long as i is less than or equal to 10.
The condition can be any
Part 3 
changes the value of the counter variable. Without this value, the loop will be considered infinite

i++  //after each execution of the loop body, 1 will be added to the loop variable i
Let's practice the for loop header entry

Another feature of the entry is that if there is only one statement in the body of the loop, then the brackets can be omitted.

Each part of the header can have multiple statements separated by commas.
Header examples:
	for ( i = 0; i < 10; i ++ ) { ... }
        //standart header
	for ( i = 0, x = 1.; i < 10; i += 2, x *= 0.1 ){ ... }   
        //in this case, we use two variables that will change after the execution of the loop body - these are the variables i and x
         // variable i changes in increments of 2:  i + = 2 - abbreviated notation from i = i + 2
         // variable x with each step increases by 0.1 times x = x * 0.1 - abbreviated x * = 0.1

Repeat N times

All programs with a for loop that we have written so far cannot be called universal. Because we set ourselves the number of repetitions of the loop body. But what if the number of repetitions depends on some other value? For example, the user himself wants to set the number of repetitions of the cycle.
What to do in this case? Everything is very simple. Instead of numeric start and end values, we can use any variables that can be calculated or set by the user.

For example, we need to display the squares of numbers from 1 to N, where the value of the variable N is entered from the keyboard by the user.
The program will look like this:
  #include <iostream> using namespace std; main() { int i,N; // i – loop variable, N - the maximum number for which we calculate the square cin>> N; for ( i = 1; i <= N; i ++) // loop: for all i from 1 to N. Variable i will sequentially take values ​​from 1 to N { cout << "Square number "<<i<<" is" <<i*i<<"\n"; // Outputting the square of a number in a specific format and moving to a new line } } When entering the loop, the statement i = 1 is executed, and then the variable i is incremented by one with each step (i ++). The loop is executed while the condition i <= N is true. In the body of the loop, the only output statement prints the number itself and its square on the screen according to the specified format.
For squaring or other low exponents, it's better to use multiplication.

Run the program and see the result of its work with different values ​​of the variable N.