Control structures allow entities to make decisions, repeat blocks of code, or iterate over collections. GPSS-Plus incorporates these structures with a simple syntax adapted to the declarative paradigm of the language.
1. IF / ELSE / ENDIF
Evaluates a condition and executes alternative blocks depending on the result.
IF (P$edad > 18 || "P$nombre"=="Eva")
MOVE {name:text1,text:"Adult or is Eva"}
ELSE
MOVE {name:text1,text:"Minor or not Eva"}
ENDIF
- The condition must be enclosed in parentheses.
- Logical operators (
&&,||) and comparisons (==,!=,>,<,>=,<=) can be used. - The entire condition is evaluated as a single expression.
2. FOREACH / ENDFOREACH
ASSIGN listaAlumnos,[{nombre:"Ana"},{nombre:"Luis"}]
FOREACH alumno IN V$listaAlumnos
MOVE {name:text1,text:"Student: P$alumno.nombre"}
ENDFOREACH
- Iterates over the elements of a collection (array or object).
- The iteration variable must be an ASSIGN.
- Can iterate over arrays or arrays of objects.
- The iteration variable is automatically created if it does not exist.
- The data source must be a valid reference via its SNA
V$(route).
Available modes
- IN: Iterates over an array.
- IN_OBJECT: Iterates over the keys of an object.
- IN_RESOURCE: Iterates over entities currently using a resource.
- IN_QUEUE: Iterates over entities waiting in a resource queue.
3. REPEAT / UNTIL
ASSIGN intento, 0
REPEAT
ASSIGN.INC intento
MOVE {name:text1,text:"Attempt number: P$intento"}
UNTIL (P$intento >= 3)
- Repeats a block until a condition is met.
- Executes at least once.
- The condition is evaluated after each iteration.
4. SWITCH / CASE / ENDCASE / ENDSWITCH
Allows execution of specific blocks depending on the value of an expression, similar to switch-case in other languages.
5. WHILE / ENDWHILE
Executes a block while a condition remains true. Unlike REPEAT, the condition is evaluated before each iteration.
Example
/* Control Structures: FOREACH and REPEAT
Iteration over arrays, objects and resources.
*/
POSITION {NAME:POS1,X:651,Y:480}
INITIAL posY,400
GRAPHIC {NAME:Text1,Type:TEXT,X:324,Y:X$posY - 0}
GRAPHIC {NAME:Text2,Type:TEXT,X:324,Y:X$posY - 20}
GRAPHIC {NAME:Text3,Type:TEXT,X:324,Y:X$posY - 40}
GRAPHIC {NAME:Text4,Type:TEXT,X:324,Y:X$posY - 60}
GRAPHIC {NAME:Text5,Type:TEXT,X:324,Y:X$posY - 80}
GRAPHIC {NAME:Text6,Type:TEXT,X:324,Y:X$posY - 160,color:#000000}
FACILITY {NAME:Facility1,X:382,Y:483,capacity:3}
START 100
GENERATE 10,0 {name:GEN1,X:110,Y:477}
ASSIGN edad,2
ASSIGN nombre,"Eva"
; IF (P$nombre=="Eva") -> Incorrect expression, missing quotes
IF ("P$nombre"=="Eva")
MOVE {name:Text1, text:"Correct expressions"}
ENDIF
IF (P$edad > 18 || V$nombre=="Eva")
MOVE {name:Text1, text:"Correct expressions"}
ENDIF
ASSIGN intentos, 0
MOVE {name:Text1, text:"Current color: P$valor"}
; FOREACH: iterate through all preferences
ASSIGN gustos_luis,""
ASSIGN persona, {nombre:"Luis"
, gustos:["leer", "música", "viajar", "dormir"]
, ciudad:"Madrid"
, pais:"España"
}
FOREACH gusto, IN, V$(persona.gustos)
ASSIGN gustos_luis,"P$gustos_luis P$gusto"
ENDFOREACH
MOVE {name:Text1, text:"Luis's preferences: P$gustos_luis"}
ASSIGN claves_valores,""
FOREACH clave, IN_OBJECT, V$(persona)
ASSIGN claves_valores,"P$(claves_valores) \n P$clave P$(persona.P$clave)"
ENDFOREACH
MOVE {name:Text6, text:"Keys and values of persona:\n P$(claves_valores)"}
ASSIGN entidades_cola,""
FOREACH entidad, IN_QUEUE, Facility1
ASSIGN entidades_cola,"P$entidades_cola P$entidad"
ENDFOREACH
MOVE {name:Text3, text:"entities in queue: P$entidades_cola"}
ASSIGN entidades_dentro,""
FOREACH entidad, IN_RESOURCE, Facility1
ASSIGN entidades_dentro,"P$entidades_dentro P$entidad"
ENDFOREACH
MOVE {name:Text4, text:"entities inside: P$entidades_dentro"}
ASSIGN intento, 0
ASSIGN maximo, 3
ASSIGN intentos,""
REPEAT
ASSIGN.INC intento
ASSIGN intentos,"P$intentos P$intento"
UNTIL (P$intento >= P$maximo)
MOVE {name:Text5, text:"attempts: P$intentos"}
ADVANCE 10,0 {to:Facility1}
SEIZE Facility1
ADVANCE 20,30
RELEASE Facility1
ADVANCE 100,0 {TO:POS1}
ENDGENERATE 1