Ocelet

Instructions and language operators

In this section the basic instructions and language operators that can be used in a scenario, a service of an entity or an interaction function of a relation are presented.


Content

  1. Declaration and allocation
  2. Calculation operators
  3. Tests and conditional operators
  4. Loop instructions

Declaration and assignment

A variable can be declared and be assigned a value using two functions: let et fix. The first one creates a variable which can be modified subsequently, and the second creates a constant.


let

Language keyword to declare a variable.

Syntax:

let variable identifier = initial value

The variable identifier must begin with a letter (lower case preferably) but can eventually be followed by numbers, and not contain blanks or special characters.

Ocelet possesses a mechanism for type inference, that is, it can infer the type of the variable from the value that is assigned to it.

Example:

let t=0             // Builds a variable of type Integer, initialized with 0
let v = 12.4764     // Builds a variable of type Double
let ff = new MyEntity // Builds a variable of type MyEntity

v = v+1  // Change the value assigned to a variable v      


fix

Language keyword to declare a constant.

Syntax:

fix constant identifier = value

The constant identifier must begin with a letter (lower case preferably) but can eventually be followed by numbers, and not contain blanks or special characters.

Ocelet possesses a mechanism for type inference, that is, it can infer the type of the constant from the value that is assigned to it.

It is not possible to modify the value assigned to a constant.

Example:

fix tmax = 1000
fix ldays = List<String>|of("Monday","Tuesday","Wednesday","Thursday","Friday")
Back to top of page  

Calculation operators

Arithmetic operators

The arithmetic operators on numeric type variables. + + : addition

  • - : subtraction
  • * : multiplication
  • / : division
  • % : remainder of the integer division
  • ** : power

Example:

let a = 15.0
let b = 4.0
let c = a+b  // c is equal to 19.0
let d = a-b  // d is equal to 11.0
let e = a*b  // e is equal to 60.0
let f = a/b  // f is equal to 3.75
let g = a%b  // g is equal to 3.0
let h = a**b // h is equal to 50625.0

Concatenation

  • + : when this operator is applied to String type variables, it represents a concatenation, that is, the assembly of two texts.

Example:

let name = "Martin"
println("Good morning "+name+"!") // displays: Good morning Martin!
let name2 = name+"a"
println("Good morning "+name2+"!") // displays: Good morning Martina!
Back to top of page  

Tests and conditional operators

if ... else

The instructions if and else allow expressions to be executed only if certain conditions are met.

Syntaxes:

if (condition) expressions

The condition is of type Boolean and is written usingconditional operators. The expressions are executed only if the condition is verified.

When the keyword else is added, other expressions can be executed in case the condition is false.

if (condition) expressions else expressions

Examples:

if (temperature > 100) state = "water vapor"
if (temperature <=0 ) state = "ice"

if ((temperature > 0) && (temperature < 100)) state="liquid"

if (i >= imax) println("Threshold reached.")
  else println("Threshold not reached.")

If several expressions are to be executed following a condition, these expressions need to be put inside braces {}:

if (i >= imax) {
  println("Threshold reached.")
  i = 10
}  


Conditional operators

To make a test, or to decide whether to continue with a loop, a condition has to be evaluated. The result of the evaluation is of type Boolean and can be equal to either true or false.

The condition is written using conditional operators:

  • && : AND
  • || : OR
  • ! : NOT
  • == : Equal to
  • != : Not equal
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
Back to top of page  

Loop instructions

Loop instructions are instructions to control how a series of operations can be carried out several times, until a condition is reached.


for

Allows repeating a series of operations when the number of iterations is known beforehand, or when running through the elements of a collection.

Syntax:

for (variable:collection) expressions

The expressions will be executed as many times as there are elements in the collection. For each iteration, the variable will be assigned an element of the collection, and that variable will be available for use in the expression (or the series of expressions).

Examples:

for (i:1..10) print(i+" ") // Displays: 1 2 3 4 5 6 7 8 9 10

let lmonth = List<String>|of("March","April","May")
for (month:lmonth) {
  println("Month: "+mois)  // Displays the three months
}

Another more versatile syntax is available, to have a better control of the increment values for example :

for (let variable = initial value;stop condition ; incrementation expression) expressions

Example :

  for (let i=0 ; i <= 20 ; i=i+5) {
      println("i="+i)
  }  // --> 0 5 10 15 20 


while

Allows repeating a series of operations as long as a condition is verified. The condition is evaluated before executing the expressions. It may happen that the expressions coming after the while are never executed (for instance when the condition is false from the start).

Syntax:

while (condition) expressions

If several expressions are to be executed after the while, they must be put within braces.

Example:

let threshold = 55.0
station.temp = 20.0   // Must be initialised before the while
while(station.temp < threshold) {
    station.updateTemp()   // station.temp is updated in this function
}


do .. while

Allows repeating a series of operations as long as a condition is verified. The condition is evaluated after executing the expressions. The expressions between do and while will be executed at least once.

Syntax:

do expressions while (condition)

If several expressions are to be executed between do and while, they must be put within braces.

Example:

let threshold = 55.0
do {
  station.updateTemp()   // the update of station.temp can also be an initialisation
} while (station.temp < threshold)
Back to top of page