Module: cycles. Loop with parameter (for)


Problem

2/17

Features of the for loop

Theory Click to read/hide

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.

 

Problem

The above program displays the numbers from 1 to 10 in a column. You can verify this by running it.
Change the title of the loop so that the program displays values ​​from 20 to 30.