Module: cycles. Loop with parameter (for)


Problem

1/17

Loops in programming

Theory Click to read/hide

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.

Problem

The following program displays the word "Hello" 10 times.
Run the program and see for yourself.