Models Controls

<< Click to Display Table of Contents >>

Navigation:  MODELS > Examples >

Models Controls

 

The MODELS language supports four types of control structures

1. IF

2. FOR

3. DO

4. WHILE

 

 

1. if - then - else - elseif - endif

 

This example illustrates how to used the  IF structure in MODELS.

 

MODEL if_example

 

VAR k

 

INIT

 k:=0

ENDINIT

 

EXEC

if k=0 then

   k:=1

elsif k=1 then

  write("In first 'elsif' with k=1")

   k:=2

elsif k=2 then

  write("In second 'elsif' with k=2")

   k:=3

else

  write("In 'else' with k=3")

endif

ENDEXEC

ENDMODEL

 

 

 

2. for - to - by - do - endfor

 

This example illustrated the usage of the FOR statement in MODELS.

 

MODEL for_example

 

VAR k

 

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

 

  write("*** Begin results of model 'for_example' ***")

 

  FOR z10:=0 TO 5, 6, 7 TO 9 BY 1 -- 3 value groups: 0 to 5      : 0,1,2,3,4,5

                                  -- 6           : 6

                                  -- 7 to 9 by 1 : 7,8,9

  FOR z1:=0 TO 8 BY 2             -- 1 group of values: 0,2,4,6,8

  DO

    k:=z10*10 +z1                 -- 0,2,4,6,8,10,12,14,16,18,20,22,24,...,94,96,98

    write(k)

  ENDFOR

 

  FOR z10:=9 TO 0 by -1         -- 1 group of values: 9,8,7,6,5,4,3,2,1,0

  FOR z1:=5, 0                   -- 2 groups of single values (call it a list?): 5, 0

  DO write(z10*10 +z1)           -- 95,90,85,80,75,...,15,10,5,0

  ENDFOR

 

  FOR n:= 1 to 4

  FOR m:= n to 4

  DO write( m )                   -- 1,2,3,4, 2,3,4, 3,4, 4

  ENDFOR

 

  write("*** End results of model 'for_example' ***")

ENDIF

ENDEXEC

ENDMODEL

 

 

 

Note!

The keyword DO is needed perform commands and must be concluded with ENDFOR. Nested FOR loops do not need DO-ENDFOR.

Standard syntax:

for i:=1 to 4 do

  for j:=i to 4 do

     x:=i*j

  endfor

endfor

 

Simplified syntax:

for i:=1 to 4

  for j:=i to 4 do

     x:=i*j

  endfor

 

 

3. do - redo - enddo

 

This example illustated the DO structure in MODELS.

 

MODEL do_example                    

 

VAR k

 

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

  write("*** Begin results of model 'do_example' ***")

   k:=5

  DO

    write('k=', k, ' in loop ')

     k:=k-1

    IF k>0 THEN REDO     -- jump up to DO statement

    ENDIF

  ENDDO

  write('k=', k, ' after loop ')

  write("*** End results of model 'do_example' ***")

ENDIF

ENDEXEC

ENDMODEL

 

 

 

4. while - do - endwhile

 

This example illustrates the use of WHILE..ENDWHILE in MODELS.

 

MODEL while_example

 

VAR k

 

EXEC

IF t=timestep THEN       -- letting contents execute only at the first time step

  write("*** Begin results of model 'while_example' ***")

   k:=5

  WHILE k>0 DO

    write('k=', k, ' in loop ')

     k:=k-1

  ENDWHILE

  write('k=', k, ' after loop ')

  write("*** End results of model 'while_example' ***")

ENDIF

ENDEXEC

ENDMODEL