Instrukcje sterujące

Zdefiniowane zostały 3 typy instrukcji sterowania

Instrukcja if

Instrukcji if można użyć na 2 sposoby:

$if(condition, { 
  codeOnTrue
})

gdzie:

lub:

$if(condition, { 
  codeOnTrue
}, {
  codeOnFalse
})

gdzie:

Przykłady

$// assign true value to "condition" variable
$=(condition, true)
$if((Boolean)$condition, {
  $// If condition is true put in the HTML header
  <h3>Header content</h3>
})
$// assign 5 value to "x" variable
$=(x, 5)
$if($==((Long)$x, 7), {
  $// if "x" variable has value 7 then assign 0 value to variable "y"
  $=(y, 0)
}, {
  $// if "x" variable has not 7 valuethen assign 3 value to variable "y"
  $=(y, 3)
})
$// put in the HTML code value of "y"
$y


Instrukcja for

Instrukcja for posiada 4 konstrukcje:

$for(loopCount, {
  loopCode
}) 


gdzie:


lub:

$for(conditionParamName, { 
  loopCode 
})

gdzie:
  • ConditionParamName - nazwa zmiennej
  • loopCode -  ciąg instrukcji wykonywanych w pętli dopóki zmienna o nazwie 
    conditionParamName będzie miała wartość true

lub:

$for(paramName, loopTable, {
  loopCode
})

gdzie:

  • paramName – nazwa zmiennej
  • loopTable – tablica typu Object[]
  • loopCode – ciąg instrukcji wykonywanych dla każdego elementu tablicy loopTable
    w każdej iteracji pętli pojedynczy element tablicy loopTable zostanie przypisany zmiennej paramName

lub:

$for(paramName, loopList, {
  loopCode
})

gdzie:

  • paramName – nazwa zmiennej
  • loopList – lista typu LinkedList
  • loopCode – ciąg instrukcji wykonywanych dla każdego elementu z listy loopList
    w każdej iteracji pętli pojedynczy wiersz listy loopList zostanie przypisany zmiennej paramName
    pojedynczy wiersz listy loopList jest tablicą typu Object[]

Przykłady
$// assign value 1 to "cnt" variable
$=(cnt, 1)
$// execution of the loop 7 times
$for(7, {
  $// put in the HTML code value of "cnt"
  $cnt
  $// incerement "cnt"
  $++(cnt)
})

$// assign value 1 to "ctr" variable
$=(ctr, 1)
$// assirn true to "loop" variable
$=(loop, true)
$// util loop is true do instruction
$for(loop, {
  $// put in the HTML code value of "ctr"
  $ctr
  $// increment ctr by 1
  $++(ctr)
  $// if value of "ctr" is greater then 4 then assign false to "ctr" variable
  $if($>((Long)$ctr, 4), { $=(loop, false) })
}) 
$// assign ["html","xml","txt"] table to variable "extensions"
$=(extensions, ["html","xml","txt"])
$// for each element in table do instructions
$for(i, $extensions[], {
  $// put in the HTML code the current array element
  $i
})
$// assign the result of SQL query to "users" variable
$=(users, $sql.read("select id, login from p_users order by login"))
$// for each element in "users" list do instructions
$for(i, (LinkedList)$users, {
  $// put in the HTML code value of current list line
  $// (first element is the id, and the second login)
  $i[0] $i[1]
})

Instrukcja try

Instrukcja try posiada 2 konstrukcje

$try({
  tryCode
}, {
  catchCode
})

gdzie

  • tryCode – ciąg instrukcji których próba wykonania może wywołać powstanie sytuacji wyjątkowej
  • catchCode – ciąg instrukcji realizujących obsługę sytuacji wyjątkowych

lub

$try({
  tryCode
}, {
  catchCode
}, {
  finallyCode
})

gdzie

  • tryCode – ciąg instrukcji których próba wykonania może wywołać powstanie sytuacji wyjątkowej
  • catchCode – ciąg instrukcji realizujących obsługę sytuacji wyjątkowych
  • finallyCode – ciąg instrukcji wykonywanych na końcu niezależnie od tego
    czy w bloku try wystąpiła sytuacja wyjątkowa i niezależnie czy została obsłużona

Przykłady

$try({
  $// divide by zero causes an exception 
  $// and go to exception handling block
  $/(7, 0)
}, {
  $// put in the HTML code information about exception
  Error:Devided by zero!
})  
$try({
  $// try to open file
  $=(WRITER, $pipe.openWriter("/tmp/text.txt", "FILE", "ISO-8859-2"))
  $// try to write to file
  $pipe.write((java.io.Writer)$WRITER, "TEXT")
}, {
  $// displey error
  $error.getMessage()
}, {
  $// try to close file
  $try({ $pipe.closeWriter((java.io.Writer)$WRITER) }, {})
})