Difference between revisions of "Recital Flow Control and Looping"

From Recital Documentation Wiki
Jump to: navigation, search
(Recital foreach loops)
(Recital foreach loops)
Line 95: Line 95:
  
 
===Recital foreach loops===
 
===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.
+
The [[FOREACH|foreach...endfor]] command is used to iterate over each element in an array or member in an object or associative array.
 
   
 
   
 
<pre>
 
<pre>

Revision as of 15:38, 14 January 2010

Recital Flow Control and Looping

Recital 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

 

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

 

Recital Looping Statements

Recital for loops

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

Example

 

Recital do while loops

The do while command repeats the commands between the do while and the enddo statement, until the specified <condition> becomes false (.F.).

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


If the specified <condition> result is .T., then all commands within the DO WHILE loop will be executed. If the specified condition returns an .F., Recital will skip down to the first statement following the ENDDO to continue execution.

Note: Use of redefined macros in the body of the loop is not supported. [edit] EXIT

If an EXIT statement is encountered then the DO WHILE loop is exited. [edit] LOOP

If a LOOP statement is encountered, then control returns to the head of the DO WHILE loop. [edit] ENDDO

The ENDDO statement terminates the DO WHILE loop. Commands within the DO WHILE must be properly nested.

Example

 

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> | foreach <array> as <key> => <value>
<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

Breaking a Loop

Skipping Statements in a Loop

Summary