current stable:
0.99.6
unstable:
cvs (0.99.7)
General
  Home / News
  About
  Contact
  The Team

Obtaining
  Download
  Source Tarball
  CVS Web View
  Misc. Files

Documentation
  Introduction
The Manual
  Download [html]
  Download [pdf]
  View Online
API
  Download [html]
  View Online
Resources
  Script Examples

Developer
  Introduction
Developer Guide
  Download [html]
  Download [pdf]
  View Online
Ferite C API
  Download [html]
  View Online




Open Source Approved

SourceForge Logo
KwMap.net - browse the Keyword Map of ferite.org

[previous] Statements[up][toc]Functions [next]


Control Structures

Ferite contains methods for changing the flow of a program, these are called control structures and are detailed below. The control structures can be placed within each other wihtout worry of mistake (ferite is clever like that!). Most of the structures follow the same rules as their counterparts in other languages although there are some differences.

if-then-else

This allows for the conditional execution of ferite scripts based upon the results of a test. The syntax is as follows:

    Type one:
    
    	if ( expression ) {
    		statements if the expression is true
    	}
    
    Type two: (with an else block)
    
    	if ( expression ) {
    		statements if the expression is true
    	} else {
    		statements if the expression is false
    	}
    					

It is also not necessary to place braces around the statement block if it's only one statement.

When execution is happening the expression gets evaluated and then it's truth value is determined, if it's true then the first block is executed. If an else block exists then it will be executed if the expression evaluates to false.

Example:

    if ( a < b )
    	Console.println( "A is less than B" );
    
    if ( b > c ) {
    	Console.println( "B is greater than C" );
    	Console.println( "This could be fun." );
    } else {
    	Console.println( "It's all good." );
    }
    					

while Loop

This construct provides a method of looping, and is used as follows:

    while ( expression ) {
    	statements if the expression is true
    }
    					

The body of the while construct will only be executed while the expression evaluates to true. The expression is evaluated upon every iteration.

    number n = 0;
    while ( n < 10 ) {
    	Console.println( "$n" );
    }
    					

The above code will loop round while n is less than 10. On each iteration of the loop the value of n will be printed out.

for Loop

This construct provides a more controlled method of looping and is also ferite's most complicated loop. It's syntax is as follows:

    for ( initiator; test; increment ) {
    	statements if the expression is true
    }
    					

The initiator expression is executed unconditionally at the beginning of the loop. The loop will continue to loop until the test expression evaluates to false, and the increment expression is evaluated at the end of each loop.

As with C, each of the expressions may be empty, this will cause them to evaluate to true (therefore causing the loop to continue forever if there is no test expression).

Example:

    number i = 0;
    for ( i = 0; i < 10; i++ )
    	Console.println( "I equals " + i ); // print out the value of i
    					

foreach Loop

This construct provides a powerfull mechanism to itterate over data structures in a loop form. It can handle strings, arrays and objects. There are two forms, the first can be used on all three data types and the second is to itterate over an array's hash.

First Version

    foreach( value, data ){
            statements to execute
    }
    					

This is used to itterate over the data values. value is the variable that the current value should be put in (NOTE: the variable must of the correct type otherwise an exception will be thrown). data is the item to itterate on. For a string, foreach set value to be a string containing each character within data. For an array, foreach will set value to be the same as each value within the array. When handling objects, foreach will call a method called next on the object. foreach will keep looping setting value as the return value from the method call, unless the return value is void at which point the loop will stop.

Second Version

    foreach( key, value, data ){
            statements to execute
    }
    					

This version only works on arrays with hash values. It will itterate through each of the keys within the array's hash, setting key with the value of the hash key and value to the value at that hash key.

Examples:

    array a = [ "Hello World", "From Chris" ];
    string value = "";
    
    foreach( value, a ){
      Console.println( "Value = $value" );
    }
    					

This will print out:

    Value = Hello World
    Value = From Chris
    					

The second form works in much the same way. It should be noted that if the value for the value part of the foreach loop is of type void, it will be reset to void on every iteration. This allows you to go through an array with different types without throwing an exception.

do .. while Loop

The do .. while loop is a variation of the while loop, the one difference being that it guarantees at least one execution of it's body. It will only then complete looping until the expression evaluates to false. It's syntax:

    do {
    	statements if the expression is true
    } while ( expression )
    					

iferr-fix-else

This control structure provides the exception handling to within ferite. It is similar in a way to the try-catch-finally structure within Java but with one main difference. Within Java the finally block is always executed regardless of whether or not an exception occurs, in ferite the else block only gets executed if no exception occurs. The fix block is called when an exception occurs. This control structure is used as follows:

    iferr {
    	statements
    } fix {
    	statements to clean up incase of an exception
    } else {
    	statements if no exception has occurred
    }
    					

It is possible to nest iferr-fix-else blocks. When an exception does occur a global variable called err is instantiated. This has two attributes, string str and number num - these provide information on the error that occurred. Exceptions are propagated up through the system until a handler is found or the program has a forced termination.

switch statement

This allows you to write blocks of code that are executed when an expression holds a certain value. This is rougly equivelent to doing a number of successive if blocks. But it's cleaner and tidier, it takes the form of:

    switch ( expression ) {
    	case expression:
    		... code ...
    	... more case blocks ...
    	default:
    		... code ...
    }
    					

When the switch statement is evaluated the expression at the top is executed and it's value saved. case blocks are evaluated to see whether the result of their expression matches that of the first expression. If it does - the code is executed until the end of the switch statement (including other case blocks). To only execute the one block you have to use break, this will cause the execution to jump to the end of the switch statement. If you use continue, the first expression will be re-evaluated effectively causing the switch statement to start again. If there are no matches the the default block will be executed. This may sound quite complicated and is probably best described using an example:

    switch( input ) {
    	case 0:
    		Console.println( "case 0" );
    	case 1:
    		Console.println( "case 1" );
    		break;
    	case 2:
    		input++;
    		Console.println( "case 2" );
    		continue;
    	case "3":
    		Console.println( "case 3" );
    		break;
    	default:
    		Console.println( "default" );
    }
    
    When input = 0, the following will be output:
    
    case 0
    case 1
    
    (Because there is no 'break' the execution keeps going into
     the next case block)
    
    When input = 1, the following will be output:
    
    case 1
    
    (Break causes execution to leave the switch block)
    
    When input = 2, the following will be output:
    
    case 2
    default
    
    (You would expect 'case 3' to be output - but the catch for
     that is a string with the character 3 in it)
    
    When input = "3", the following will be output:
    
    case 3
    
    (Break causes execution to leave the switch block)
    
    When input is anything else:
    
    default
    					

It is very important to note that you can use any valid expression within the case expression, it can even have different types than the first switch expression, there wont be any exceptions thrown. This makes switch a very powerfull construct. It is also not necessary to supply a default block if you do not wish to have one.

break

break will end the current for, while, do .. while, switch or foreach loop it is executed in.

continue

continue will cause execution flow to jump to the beginning to the current for, while, do .. while, switch or foreach loop it is executed in.



[previous] Statements[up][toc]Functions [next]
ferite et al © 2000-2004, Chris Ross