JAVASCRIPT


  ITERATIVE INSTRUCTIONS.

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



  LOOP „for”

  for (initial_value; condition; action) instructions;

Example:

function add(n)
{
  var sum=0;
  for (var i=1; i<=n; i++)
  {
  sum=sum+i;
  }
  return sum;
}

Function „add” function calculates the sum of natural numbers 1+2+...+n for the given n.



  LOOP „while”

  while (condition) {instructions}

We use it when we do not know the exact number of repetitions, but it is possible that the given instruction will not be executed even once. Instructions are executed as long as the condition is true.

Example:

// determining consecutive digits of the number in binary notation
function binTostr(n)
{
  var c;
  var s="";
  while (n>0)
  {
  c=n%2;
  s=s+c.toString();
  n=Math.floor(n/2);
  }
  return s;
}