Rogue Wave Script 2.0 Language Reference > Statements
 
Statements
The topics are:
*Conditional Statement (if)
*Loops (while, for, for..in, break, continue)
*Variable Declaration (var)
*Function Definition (function, return)
*Default Value (with)
Conditional Statement
The conditional (if) statement has the following syntax:
Table F.10   Rogue Wave Script 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 yields a non-boolean value, this value is converted to a boolean.
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")
  }
Loops
The loop statements have the following syntax:
Table F.11   Rogue Wave Script Loop Statement 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 yields a non-boolean value, this value is converted to a boolean.
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 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 yields a non-boolean value, this value is converted to a boolean.
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 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..in loop, and continue the execution at the statement immediately following the loop. This statement cannot be used outside of 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..in loop, and continue the execution of the loop with the next iteration. This statement cannot be used outside of a loop.
Example:
  for (var i=0; i < a.length; i++) {
    if (a[i] < 0) continue
    writeln("A positive number: ", a[i])
  }
Variable Declaration
The variable declaration has the following syntax:
Table F.12   Rogue Wave Script Variable Declaration Syntax 
Syntax
Effect
var decl1, ..., decln
where each decli has the form
variable [ = expression ]
Declare each variable as a local variable. If an expression is provided, it is evaluated and its value is assigned to the variable as its initial value. Otherwise, the variable is set to the undefined value.
Examples:
  var x
  var name = "Joe"
  var average = (a+b)/2, sum, message="Hello"
var inside a function definition
When var is used inside of a function definition, the declared variables are local to the function, and they hide any global variables with the same name; actually, they have the same status as function arguments.
For example, in the following program, the variables sum and res are local to the average function, as well as the arguments a and b; when average is called, the global variables with the same names, if any, are temporarily hidden until the function is exited:
  function average(a, b) {
    var sum = a+b
    var res = sum/2
    return res
  }
Variables declared with var at any place in a function body have a scope which is the entire function body. This is different from local variable scope in C or C++.
For example, in the following function, the variable res declared in the first branch of the if statement is used in the other branch and in the return statement:
  function max(x, y) {
    if (x > y) {
      var res = x
    } else {
      res = y
    }
    return res
  }
var outside a function definition
 
When var is used outside of a function definition, that is, at the same level as function definitions, the declared variables are local to the current program unit. A program unit is a group of statements which are considered a whole; the exact definition of a program unit depends on the application in which Rogue Wave Script is embedded. Typically, a script file loaded by the application is treated as a program unit. In this case, variables declared with var at the file top level are local to this file, and they hide any global variables with the same names.
For example, suppose that a file contains the following program:
  var count = 0
  
  function NextNumber() {
    count = count+1
    return count
  }
When this file is loaded, the function NextNumber becomes visible to the whole application, while count remains local to the loaded program unit and is visible only inside it.
It is an error to declare the same local variable twice in the same scope. For example, the following program is incorrect because res is declared twice:
  function max(x, y) {
    if (x > y) {
        var res = x
    } else {
        var res = y // Error
    }
    return res
  }
Function Definition
A function definition has the following syntax:
Table F.13   Rogue Wave Script Function Definition 
Syntax
Effect
[ static ]
function name(v1,...,vn)
{
statements }
Defines a function name with the given parameters and body. A function definition can only take place at the top level; function definitions cannot be nested.
When the function is called, the variables v1,...,vn are set to the corresponding argument values. Then, the statements are executed. If a return statement is reached, the function returns the specified value; otherwise, after the statements are executed, the function returns the undefined value.
The number of actual arguments does not need to match the number of parameters: If there are less arguments than parameters, the remaining parameters are set to the undefined value; if there are more arguments than parameters, the exceeding arguments are ignored.
Independently of the parameter mechanism, the function arguments can be retrieved using the arguments keyword.
Defining a function name is operationally the same as assigning a specific function value to the variable name; thus a function definition is equivalent to:
     var name = some function value
The function value can be retrieved from the variable and manipulated like any other type of value. For example, the following program defines a function add and assigns its value to the variable sum, which makes add and sum synonyms for the same function:
  function add(a, b) {
    return a+b
  }
  
  sum = add
Without the static keyword, the defined function is global and can be accessed from the whole application. With the static keyword, the function is local to the current program unit, exactly like if name was declared with the var keyword:
     var name = some function value
return [ expression ]
Returns the value of expression from the current function. If expression is omitted, returns the undefined value. The return statement can only be used in the body of a function.
Default Value
A default value is used with the following syntax:
Table F.14   Rogue Wave Script Default Value  
Syntax
Effect
with (expression)
  statement
Evaluate expression, then execute statement with the value of expression temporarily installed as the default value.
When evaluating a reference to an identifier name in statement, this identifier is first looked up as a property of the default value; if the default value does not have such a property, name is treated as a regular variable.
For example, the following program displays "The length is 3", because the identifier length is taken as the length property of the string "abc".
  with ("abc") {
    writeln("The length is ", length)
  }
With statements can be nested; in this case, reference to identifiers are looked up in the successive default values, from the innermost to the outermost with statement.

Version 5.7
Copyright © 2013, Rogue Wave Software, Inc. All Rights Reserved.