cycles. Loop with parameter (for)


Let's imagine a situation where we need to display the same word on the screen, let's say the word "HELLO" ;, 10 times. What should we do?
You can take and write the command 10 times Console.WriteLine("HELLO");

But what if you need 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 that the user chooses how many times to display information on the screen? 

To cope with this task, we can use a special construction called loop.

A loop is an algorithmic construction in which a certain sequence of commands is repeated several times.

In the C# programming language, there are two kinds of loops: a variable loop (for) and a conditional loop (while and do...while)

Let's start our acquaintance with cycles from the first type.

A loop with a variable or with a known number of steps (for).

It often happens that we know the number of repetitions of some action, or we can serif">calculate
number of repetitions using the data we know. Some programming languages ​​have a command that in Russian sounds like repeat (number of times) - that is, we can specify the exact number of repetitions. 

It is interesting to see how this cycle works at the machine level:
1. A certain memory cell is allocated in memory and the number of repetitions is written to it.
2. When the program executes the body of the loop once, the contents of this cell (counter) is decremented by one.
3. The loop ends when this cell is zero.

In the C# programming language, there is no such construct, but rather the for construct.  

The general form of the loop operator for is as follows:
for (/*expression1*/; /*expression2*/; /*expression3 */)
{
      /*one statement or block of statements - loop body*/;
}
This construct requires us to:
1. Explicitly allocated a memory cell that will be a counter, and set its initial value.
2. We have written a condition under which the loop body will be executed.
3. Specify how the value in this cell will change.

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.

for
loop
The for loop is a means of stepping through repeated actions. Let's take a closer look at how it works.

Typically, parts of the for loop take the following steps: 
1. Setting the initial value. 
2. Performing a condition test to continue the loop. 
3. Perform loop actions. 
4. Update the value(s) used in the test. 
and then steps 2-4 are repeated until the condition is met. As soon as the condition becomes false, the loop terminates and the statement following the for loop statement is executed.
 
General form of the loop  for (/* expression 1*/; /* expression 2*/; /* expression 3*/ ) {       /* one statement or block of statements - loop body */; }

Expression 1 responsible for setting the initial value of the loop variable (counter), ends with a semicolon.
For example :
  • i=0; // loop variable i set initial value equal to zero - should be declared before the loop
  • int i=0; // the i variable can be declared immediately in the loop header, but in this case, it will be erased from memory after the loop runs
  • ;  // there is no initialization and declaration of the cycle variable at all, in this case, it can be declared before the cycle
Expression 2 - this is the condition for continuing the for loop, it is tested for truth. For example,

i <= 10  // the loop will run as long as the variable i is less than or equal to 10.
The condition can be anything.

Expression 3 changes the value of the counter variable. Without this value, the loop will be considered infinite. For example,

i++;  // each time the loop body completes, i is incremented by 1.

 

There can be multiple operators in each heading part, separated by commas.< /span>
Heading examples:

for ( int i = 0; i < 10; i + + ) { ... } - standard title

for ( int i = 0, x = 1; i < 10; i += 2, x *= 2 ){ ... }  
         // 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 - short for i=i+2
         // variable x increases by 0.1 times with each step x=x*2 - abbreviated х*=2

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.