C++


  ITERATIVE INSTRUCTIONS

Iteration (Latin iteratio - repetition) - repetition activity (usually multiple) the same instruction (or multiple instructions) in a loop.


  LOOP „for”

  for (initial_value; condition; action) instrukcja;


  for (k=1; k<2011; k++) cout <<”Kwidzyn”;

will print 2010 subtitles, each in a separate line. The variable k is of the total type and is increased after each loop by 1 (it can be changed in any other way, e.g. k+=2, k*=5, k=2*j-7, etc.). k++ means increasing the control variable k by 1 after executing the instruction and is equivalent to the instruction k=k+1.


  for (n=1; n!=1024; n*=2) cout <<n<<”,”;

will print a sequence of numbers 1, 2, 4, 8, 16, 32, 64, 128, 256, 512,

The passage of the loop is carried out by means of an iterative variable. First the initial value is given, then the logical condition is checked and if it is true, then the given instruction is executed, then the iterative variable (so called loop counter) is changed at the end and the condition is checked again, and so on.

Each of the elements of the forum loop may be omitted, but it must contain semicolons. The control is taken over by the if instruction. Exiting the loop causes the break instruction.

Example:

   licz=0; maks=5;
   for ( ; ; )
   {
    if (licz < maks)
   {cout <<"ABCDEFG" <<endl; licz++;}
   else break;
   }

On the screen we will see five lines of text with "ABCDEFG".