Provides a reference for the syntax of conditional statements
in OPL.
Loops
Table 1. Conditional statement syntax
| Syntax |
Effect |
if (expression) statement1
[else statement2]
|
Evaluate expression ; if it is true, execute statement1 ;
otherwise, if statement2 is provided, execute statement2.
If expression gives
a non-Boolean value, this value is converted to a Boolean value.
Examples:
if
(a == b) writeln("They are equal") else writeln("They are not equal")
if (s.indexOf("a") < 0) { write("The string ", s) writeln(" doesn’t
contains the letter a") } |
Table 2. Loop syntax
| Syntax |
Effect |
while (expression)
statement
|
Execute statement repeatedly as long as expression is true.
The test takes place before each execution of statement.
If expression gives
a non-Boolean value, this value is converted to a Boolean value.
Examples:
while
(a*a < b) a = a+1 while (s.length) { r = s.charAt(0)+r s = s.substring(1)
} |
for ( [ initialize ] ;
[ condition
] ;
[ update ] )
statement
where condition and update are
expressions, and initialize is either an expression or has
the form:
var variable = expression
|
Evaluate initialize once, if present. Its
value is ignored. If it has the form:
var variable = expression then variable is
declared as a local script variable and initialized as in the var statement.
Then,
execute statement repeatedly as long as condition is
true. If condition is omitted, it is taken to be
true, which results in an infinite loop. If condition gives a non-Boolean
value, this value is converted to a Boolean value.
If present, update is
evaluated at each pass through the loop, after statement and
before condition. Its value is ignored.
Example:
for
(var i=0; i < a.length; i++) { sum = sum+a[i] prod = prod*a[i]
} |
for ( [ var ] variable in expression)
statement |
Iterate over the properties of the value of expression :
for each property, variable is set to a string representing
this property, and statement is executed once.
If
the var keyword is present, variable is
declared as a local script variable, as with the var statement.
For
example, the following function takes an arbitrary value and displays
all its properties and their values:
function printProperties(v)
{ for (var p in v) writeln(p, " -> ", v[p]) } Properties
listed by the for..in statement include method properties,
which are merely regular properties whose value is a function value.
For example, the call printProperties("foo") would
display:
length -> 3 toString -> [primitive method toString]
substring -> [primitive method substring] charAt -> [primitive
method charAt] etc The only properties which are not listed by for..in loops
are the numeric properties of arrays.
|
break |
Exit the current while, for or for..i n
loop, and continue the execution at the statement immediately following
the loop. This statement cannot be used outside a loop.
Example:
while
(i < a.length) { if (a[i] == "foo") { foundFoo = true break
} i = i+1 } // Execution continues here |
continue |
Stop the current iteration of the current while, for or for..i n
loop, and continue the execution of the loop with the next iteration.
This statement cannot be used outside a loop.
Example:
for
(var i=0; i < a.length; i++) { if (a[i] < 0) continue writeln("A
positive number: ", a[i]) } |