LOOP … ENDLOOP

English (Español a continuación)

The LOOP… ENDLOOP command determines the number of times a calculation is executed.

The syntax of this command is:

  • LOOP (Number, [Break])
  • Calculation;
  • ENDLOOP

Example:

  • FIX
    • LOOP (10)
      • Profits = Income – Expenses;
      • Expenses = 0.1 * Profits;
    • ENDLOOP
  • ENDFIX

“Number”: number of times the calculation contained in the LOOP… ENDLOOP structure is executed.

«Break»: this parameter is optional and determines that the LOOP ends before completing the number of iterations that has been established.

  • A temporary variable is used that is initially set to zero.
  • A condition is included within the LOOP and when this is fulfilled the variable changes value (takes for example the value 1) and the LOOP is interrupted at the end of that iteration.

The parameter «Number» has to be a number, it cannot be a variable with a numeric value. If we want the LOOP to be executed a number of times that depends on the value of a variable, what we can do is:

  • Assign the parameter «Number» a high value.
  • Include within the LOOP a temporary variable whose value increases by one unit in each iteration and when it reaches a certain value, it leads to the fulfillment of the condition that executes the “Break”.

«Calculation»: these are the calculation instructions that are executed within the LOOP structure.

  • They are usually calculations of members in which there is dependency: for example, “A” depends on “B” and “B” depends on “A”; performing iterations it is possible to refine the result.

For example:

  • Profits = Income – Expenses;
  • Expenses = 0.1 * Profits;

Some considerations:

a) Within a LOOP structure more than one calculation can be included:

  • LOOP (10)
    • Profits = Income – Expenses;
    • Expenses = 0.1 * Profits;
  • ENDLOOP

b) LOOP … ENDLOOP structures can be nested within other structures:

  • LOOP (5)
    • LOOP (10)
      • Profits = Income – Expenses;
      • Expenses = 0.1 * Profits;
    • ENDLOOP
    • LOOP (10)
      • Income = 100 + 10;
    • ENDLOOP
  • ENDLOOP

c) This structure can be included within a FIX:

  • FIX (Germany, Product_A, ”Year_2021”)
    • LOOP (10)
      • Profits = Income – Expenses;
      • Expenses = 0.1 * Profits;
    • ENDLOOP
  • ENDFIX

d) Within a LOOP… ENDLOOP structure an IF… ELSEIF structure can be included:

  • LOOP (10)
    • IF (@ISMBR (Germany))
      • Profits = Income – Expenses;
      • Expenses = 0.1 * Profits;
    • ELSE
      • Profits = Income – Expenses;
      • Expenses = 0.2 * Profits;
    • ENDIF
  • ENDLOOP

e) Within this structure, the CLEARDATA and DATACOPY commands can be used, which cannot be included in an IF structure.

Let’s look at some examples:

Example A

  • VAR STOP = 0;
  • FIX (“2020”, Germany, Product_A)
    • LOOP (30, STOP)
      • Budget_Sales (
        • January = Sales -> January -> 2019 * 1.01;
      • )
      • Budget_Sales (
        • IF (Budget_Sales> 100)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
      • )
    • ENDLOOP Budget
  • ENDFIX

In this example, we calculate the January / 2020 sales target for the German market for product “A”:

  • It is calculated by multiplying the sales of the previous year by 1.01; and this calculation is repeated 30 times.
  • If the sales target exceeds 100 in this calculation, the LOOP ends.

Example B

  • VAR STOP = 0;
  • FIX («2020», January, Product_A)
    • LOOP (10, STOP)
      • Budget_Sales (
        • Germany = Germany -> Sales -> January -> 2019 * 1.01;
        • France = France -> Sales -> January -> 2019 * 1.03;
      • )
      • Budget_Sales (
        • IF (Budget _Sales -> France-> Budget_Sales -> Germany)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
        • )
    • ENDLOOP
  • ENDFIX

In this example, we calculate the January / 2020 sales target for product A for the German market and the French market:

  • The target for the German market is calculated by multiplying the previous year’s sales by 1.01, and the French market by 1.03; and this calculation is repeated 10 times.
  • If the objective of the French market exceeds that of the German market, the LOOP ends.

Example C

  • VAR STOP = 0;
  • FIX (“2020”, Product_A, Germany)
    • LOOP (25, STOP)
      • Budget_Sales (
        • IF (@ISMBR (January))
        • January + 5;
        • ELSEIF (@ISMBR (February))
        • January * 1.02;
        • ELSEIF (@ISMBR (March))
        • February * 1.02;
        • ELSEIF (@ISMBR (April))
        • March * 1.02;
        • ELSEIF (@ISMBR (May))
        • April * 1.02;
        • ELSEIF (@ISMBR (June))
        • May * 1.02;
        • ELSEIF (@ISMBR (July))
        • June * 1.02;
        • ELSEIF (@ISMBR (August))
        • July * 1.02;
        • ELSEIF (@ISMBR (September))
        • August * 1.02;
        • ELSEIF (@ISMBR (October))
        • September * 1.02;
        • ELSEIF (@ISMBR (November))
        • October * 1.02;
        • ELSEIF (@ISMBR (December))
        • November * 1.02;
        • ENDIF;
      • )
      • Budget_Sales (
        • IF (@SUMRANGE (Budget_Sales, January: December) -> 500)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
      • )
    • ENDLOOP
  • ENDFIX

In this example we distribute the sales target for product “A” for the year 2020 in the German market:

  • We establish as a condition that each month is 2% higher than the previous month; and this calculation is repeated 25 times.
  • If the sum of the monthly objectives exceeds 500 the LOOP ends.

Español

El comando LOOP … ENDLOOP determina el número de veces que se ejecuta un cálculo.

La sintaxis de este comando es:

  • LOOP(Número,[Break])
  • Cálculo;
  • ENDLOOP

Ejemplo:

  • FIX
    • LOOP(10)
      • Rdos = Ingresos – Gastos;
      • Gastos = 0.1 * Rdos;
    • ENDLOOP
  • ENDFIX

“Número”: número de veces que se ejecuta el cálculo contenido en la estructura LOOP … ENDLOOP.

“Break”: este parámetro es opcional y determina que el LOOP finalice antes de completar el número de iteraciones que se ha establecido.

  • Se utiliza una variable temporal a la que inicialmente se le da el valor cero.
  • Dentro del LOOP se incluye una condición y cuando esta se cumple la variable cambia de valor (toma por ejemplo el valor 1) y el LOOP se interrumpe al final de esa iteración.

El parámetro “Número” tiene que ser un número, no puede ser una variable con un valor numérico. Si nos interesa que el LOOP se ejecute un número de veces que dependa del valor de una variable, lo que se puede hacer es:

  • Asignar al parámetro “Número” un valor elevado.
  • Incluir dentro del LOOP una variable temporal cuyo valor se vaya incrementando en una unidad en cada iteración y cuando alcance determinado valor conlleve el cumplimiento de la condición que ejecute el “Break”.

“Cálculo”: son las instrucciones de cálculo que se ejecutan dentro de la estructura LOOP.

  • Suelen ser cálculos de miembros en los que hay dependencia: por ejemplo, “A” depende de “B” y “B” depende de “A”; realizando iteraciones se consigue afinar el resultado.

Por ejemplo:

  • Rdos = Ingresos – Gastos;
  • Gastos = 0.1 * Rdos;

Algunas consideraciones:

a) Dentro de una estructura LOOP se pueden incluir más de un cálculo:

  • LOOP(10)
    • Rdos = Ingresos – Gastos;
    • Gastos = 0.1 * Rdos;
  • ENDLOOP

b) Se pueden anidar estructuras LOOP … ENDLOOP dentro de otras estructuras:

  • LOOP(5)
    • LOOP(10)
      • Rdos = Ingresos – Gastos;
      • Gastos = 0.1 * Rdos;
    • ENDLOOP
    • LOOP(10)
      • Ingresos = 100 + 10;
    • ENDLOOP
  • ENDLOOP

c) Esta estructura se puede incluir dentro de un FIX:

  • FIX(Alemania,Product_A,”Año_2021”)
    • LOOP(10)
      • Rdos = Ingresos – Gastos;
      • Gastos = 0.1 * Rdos;
    • ENDLOOP
  • ENDFIX

d) Dentro de una estructura LOOP … ENDLOOP se puede incluir una estructura IF …  ELSEIF:

  • LOOP(10)
    • IF(@ISMBR(Alemania))
      • Rdos = Ingresos – Gastos;
      • Gastos = 0.1 * Rdos;
    • ELSE
      • Rdos = Ingresos – Gastos;
      • Gastos = 0.2 * Rdos;
    • ENDIF
  • ENDLOOP

e) Dentro de esta estructura se pueden utilizar los comandos CLEARDATA y DATACOPY, que no se pueden incluir en una estructura IF.

Veamos algunos ejemplos:

Ejemplo A

  • VAR STOP = 0;
  • FIX(“2020”,Alemania,Product_A)
    • LOOP(30,STOP)
      • Obj_Ventas(
        • Enero = Ventas -> Enero -> 2019 * 1.01;
      • )
      • Obj_Ventas(
        • IF(Obj_Ventas > 100)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
      • )
    • ENDLOOP
  • ENDFIX

En este ejemplo, calculamos el objetivo de ventas de enero/2020 del mercado alemán del producto “A”:

  • Se calcula multiplicando las ventas del año anterior por 1,01; y este cálculo se repite 30 veces.
  • Si en este cálculo el objetivo de ventas llega a superar 100 el LOOP finaliza.

Ejemplo B

  • VAR STOP = 0;
  • FIX(“2020”,Enero,Product_A)
    • LOOP(10,STOP)
      • Obj_Ventas(
        • Alemania = Alemania -> Ventas -> Enero -> 2019 * 1.01;
        • Francia = Francia -> Ventas -> Enero -> 2019 * 1.03;
      • )
      • Obj_Ventas(
        • IF(Obj_Ventas -> Francia -> Obj_Ventas -> Alemania)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
      • )
    • ENDLOOP
  • ENDFIX

En este ejemplo, calculamos el objetivo de ventas de enero/2020 del producto A del mercado alemán y del mercado francés:

  • El objetivo del mercado alemán se calcula multiplicando las ventas del año anterior por 1,01, y el del mercado francés por 1,03; y este cálculo se repite 10 veces.
  • Si el objetivo del mercado francés supera al del mercado alemán el LOOP finaliza.

Ejemplo C

  • VAR STOP = 0;
  • FIX(“2020”,Product_A,Alemania)
    • LOOP(25,STOP)
      • Obj_Ventas(
        • IF(@ISMBR(Enero))
        • Enero + 5;
        • ELSEIF(@ISMBR(Febrero))
        • Enero * 1.02;
        • ELSEIF(@ISMBR(Marzo))
        • Febrero * 1.02;
        • ELSEIF(@ISMBR(Abril))
        • Marzo * 1.02;
        • ELSEIF(@ISMBR(Mayo))
        • Abril * 1.02;
        • ELSEIF(@ISMBR(Junio))
        • Mayo * 1.02;
        • ELSEIF(@ISMBR(Julio))
        • Junio * 1.02;
        • ELSEIF(@ISMBR(Agosto))
        • Julio * 1.02;
        • ELSEIF(@ISMBR(Septiembre))
        • Agosto * 1.02;
        • ELSEIF(@ISMBR(Octubre))
        • Septiembre * 1.02;
        • ELSEIF(@ISMBR(Noviembre))
        • Octubre * 1.02;
        • ELSEIF(@ISMBR(Diciembre))
        • Noviembre * 1.02;
        • ENDIF;
      • )
      • Obj_Ventas(
        • IF(@SUMRANGE(Obj_Ventas,Enero:Diciembre) -> 500)
        • STOP = 1;
        • ELSE
        • STOP = 0;
        • ENDIF;
      • )
    • ENDLOOP
  • ENDFIX

En este ejemplo distribuimos el objetivo de ventas del producto “A” del año 2020 del mercado alemán:

  • Establecemos como condición que cada mes sea un 2% superior al del mes anterior; y este cálculo se repite 25 veces.
  • Si la suma de los objetivos mensuales supera 500 el LOOP finaliza.
Anuncio publicitario