Difference between revisions of "Recital Flow Control and Looping"

From Recital Documentation Wiki
Jump to: navigation, search
(Recital Conditional Statements)
(The Recital if Statement)
 
(7 intermediate revisions by one user not shown)
Line 3: Line 3:
 
Recital supports the following conditional statements:
 
Recital supports the following conditional statements:
  
* [[DO CASE|do case]]
+
* [[DO CASE|do case...enddo]]
* [[IF|if]]
+
* [[IF|if...endif]]
  
 
===Recital do case statements===
 
===Recital do case statements===
Line 28: Line 28:
  
 
<code lang="recital">
 
<code lang="recital">
 +
do case
 +
case month(date()) = 1 and day(date()) = 1
 +
    echo "Happy New Year\n"
 +
case month(date()) = 7 and day(date()) = 4
 +
    echo "Happy 4th July\n"
 +
otherwise
 +
  echo "Today's date is " + dtoc(date()) + "\n"
 +
endcase
 
</code>
 
</code>
  
Line 49: Line 57:
  
 
<code lang="recital">
 
<code lang="recital">
 +
if month(date()) = 1 and day(date()) = 1
 +
    echo "Happy New Year\n"
 +
elseif month(date()) = 7 and day(date()) = 4
 +
    echo "Happy 4th July\n"
 +
else
 +
  echo "Today's date is " + dtoc(date()) + "\n"
 +
endif
 
</code>
 
</code>
  
 
===Recital Looping Statements===
 
===Recital Looping Statements===
 +
Recital supports the following looping statements:
 +
 +
* [[FOR|for...next]]
 +
* [[DO WHILE|do while...enddo]]
 +
* [[FOREACH|foreach...endfor]]
 +
 
===Recital for loops===
 
===Recital for loops===
 
The [[FOR|for...next]] command repeats the commands between the ''for'' and the ''next'' statements for a specified number of times.
 
The [[FOR|for...next]] command repeats the commands between the ''for'' and the ''next'' statements for a specified number of times.
Line 70: Line 91:
  
 
<code lang="recital">
 
<code lang="recital">
 +
for i = 1 to 100 step 10
 +
    ? i * i
 +
next
 
</code>
 
</code>
  
Line 85: Line 109:
  
 
If an ''exit'' statement is encountered, the loop is exited.  If a ''loop'' statement is encountered, control returns to the head of the loop.
 
If an ''exit'' statement is encountered, the loop is exited.  If a ''loop'' statement is encountered, control returns to the head of the loop.
 +
 +
Note: when processing a series of records, remember to move the record pointer to the next required record.  The [[SKIP|skip]] command can be used do this.
 +
 +
<pre>
 +
skip <records as numeric> [in <cursor as numeric> | <alias as character>]
 +
</pre>
  
 
'''Example'''
 
'''Example'''
  
 
<code lang="recital">
 
<code lang="recital">
 +
open database southwind
 +
use order_details order orderid in 0
 +
use orders in 0
 +
total = 0
 +
do while not eof()
 +
    if shipvia = 1
 +
        skip
 +
        loop
 +
    endif
 +
    select order_details
 +
    seek orders.orderid
 +
    do while order_details.orderid = orders.orderid
 +
        total = total + (order_details.quantity * order_details.unitprice);
 +
          * (1 - order_details.discount)
 +
        skip
 +
    enddo
 +
    select orders
 +
    echo "Order total for order " + orders.orderid + " = " + total + "\n"
 +
    skip
 +
    total = 0
 +
enddo
 
</code>
 
</code>
  

Latest revision as of 12:30, 27 January 2010

Recital Flow Control and Looping

Recital Conditional Statements

Recital supports the following conditional statements:

Recital do case statements

The do case command selects one course of action out of one or more alternatives.

do case
case <condition as logical>
[<commands>]
[case <condition as logical>
[<commands>]]
[...]
[otherwise
[<commands>]]
endcase 

Each case condition is evaluated in turn. As soon as one of the conditions evaluates to true (.T.), the commands for that case are executed and any further case statements are ignored. Following execution of the commands, the program continues after the endcase statement. If an otherwise statement is present and no case condition evaluates to true, the otherwise commands are executed.

If no case condition evaluates to true, and there is no otherwise statement specified, then control skips to the next command following the endcase.

Example

do case
case month(date()) = 1 and day(date()) = 1
    echo "Happy New Year\n"
case month(date()) = 7 and day(date()) = 4
    echo "Happy 4th July\n"
otherwise
   echo "Today's date is " + dtoc(date()) + "\n"
endcase

The Recital if Statement

The if command processes commands based on the evaluation of a logical condition.

if <condition as logical>
[elseif <condition as logical>]
[else]
endif

If the result of the if condition is true (.T.), then the commands that follow up to an else, elseif or endif statement are executed.

The elseif clause can be added to the control structure allowing for the testing of more than one condition. The if...endif block is now essentially the same as the do...case structure and elseif is analogous with a case statement.

The else statement is analogous with the otherwise statement. If no previous if condition or elseif condition is true, the commands following the else statement up to the endif statement are executed.

Example

if month(date()) = 1 and day(date()) = 1
    echo "Happy New Year\n"
elseif month(date()) = 7 and day(date()) = 4
    echo "Happy 4th July\n"
else
   echo "Today's date is " + dtoc(date()) + "\n"
endif

Recital Looping Statements

Recital supports the following looping statements:

Recital for loops

The for...next command repeats the commands between the for and the next statements for a specified number of times.

for <count variable> = <start as numeric> to <end as numeric>
[step <step as numeric>]
[exit]
[loop]
next

At the beginning of the for...next loop, the count is set to the start value and is increased by 1, or the value of step if specified, for each loop.

If an exit statement is encountered, the loop is exited. If a loop statement is encountered, control returns to the head of the loop.

Example

for i = 1 to 100 step 10
    ? i * i
next

Recital do while loops

The do while command repeats the commands between the do while and the enddo statements until the specified condition becomes false (.F.).

do while <condition as logical>
[exit]
[loop]
enddo

Once the condition evaluates to false, Recital will skip down to the first statement following the enddo to continue execution.

If an exit statement is encountered, the loop is exited. If a loop statement is encountered, control returns to the head of the loop.

Note: when processing a series of records, remember to move the record pointer to the next required record. The skip command can be used do this.

skip <records as numeric> [in <cursor as numeric> | <alias as character>]

Example

open database southwind
use order_details order orderid in 0
use orders in 0
total = 0
do while not eof()
    if shipvia = 1
        skip
        loop
    endif
    select order_details
    seek orders.orderid
    do while order_details.orderid = orders.orderid
        total = total + (order_details.quantity * order_details.unitprice);
          * (1 - order_details.discount)
        skip
    enddo
    select orders
    echo "Order total for order " + orders.orderid + " = " + total + "\n"
    skip
    total = 0
enddo

Recital foreach loops

The foreach...endfor command is used to iterate over each element in an array or member in an object or associative array.

foreach <array> as <value as variable> | <array> as <key variable> => <value as variable>
<statements>
endfor

The array must be the name of an existing array or object. The value is a reference to be loaded with the contents of each element or member in turn. Using the <key> => <value> syntax, the member names of an object or associative array can also be accessed.

Examples

// static array
numbers = array(1,2,3,4,5,6,7,8,9,10)
foreach numbers as elem
    ? elem * elem
endfor
 
// associative array
private myarray = array("Name" => "Recital", "Description" => "database")
foreach myarray as key => value
    echo "key=" + key + " value=" + value + "\n"
endfor