current stable: 0.99.6 unstable: cvs (0.99.7) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Control StructuresFerite 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-elseThis allows for the conditional execution of ferite scripts based upon the results of a test. The syntax is as follows:
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:
while LoopThis construct provides a method of looping, and is used as follows:
The body of the while construct will only be executed while the expression evaluates to true. The expression is evaluated upon every iteration.
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 LoopThis construct provides a more controlled method of looping and is also ferite's most complicated loop. It's syntax is as follows:
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:
foreach LoopThis 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
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
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:
This will print out:
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 LoopThe 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:
iferr-fix-elseThis 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:
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 statementThis 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:
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:
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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ferite et al © 2000-2004, Chris Ross |