Scripting:Syntax
From STNE Wiki
Line 33: | Line 33: | ||
WriteLine("More than ten."); | WriteLine("More than ten."); | ||
} | } | ||
+ | |||
+ | == Loops == | ||
+ | There are a few kind of loop statements: For, For Each, While and Do...While loops. They work similar to For, While and Do...While loops in other programming languages. The For loop will increment a specified counter by a given step until a certain value is reached. The For Each loop iterates all elements of a CCollectionBase. The While and Do...While loops continue to loop while a boolean expression results in True. The difference between the While and Do...While loops is when the boolean expression is evaluated. While loops check the expression at the beginning of each iteration, Do...While loops check the expression at the end of each iteration. As a result, Do...While loops are always executed at least once. | ||
+ | |||
+ | An example: | ||
+ | /* This will print each number from 0 to 10. 0 and 10 included. */ | ||
+ | Var i As Integer; | ||
+ | For (i = 0 To 10) { | ||
+ | WriteLine(i); | ||
+ | } | ||
+ | |||
+ | /* This will print the name of each ship in the SRS report of the ship with NCC 123456 */ | ||
+ | Var MyShip As New CMyShip(125466); | ||
+ | Var Current As CShip; | ||
+ | For (Each Current In MyShip.SRS) { | ||
+ | WriteLine(Current.Name); | ||
+ | } | ||
+ | |||
+ | /* This will print all even numbers from 0 to 10. 0 and 10 included. */ | ||
+ | Var i As Integer; | ||
+ | For (i = 0 To 10 Step 2) { | ||
+ | WriteLine(i); | ||
+ | } | ||
+ | |||
+ | /* This will print each number from 0 to 10. 0 and 10 included. */ | ||
+ | i = 0; | ||
+ | While (i <= 10) { | ||
+ | WriteLine(i++); // i++ increases i by one and returns the old value, see [[Scripting:Operators|Operators]] for details. | ||
+ | } | ||
+ | |||
+ | /* This will also print each number from 0 to 11. 0 and 11 included! */ | ||
+ | i = 0; | ||
+ | Do { | ||
+ | WriteLine(i++); | ||
+ | } | ||
+ | While(i <= 10) |
Revision as of 18:50, 15 August 2010
This page describes the syntax of the STNE scripting engine.
As with C style languages, a statement is always terminated by a semicolon. A statement can be a function call, variable declaration or assignment.
Variable declaration
Variables are declared with the code 'Var [VarName] As VarType'. This is best illustrated with a small example:
Var WelcomeMessage As String; // Declares a variable named 'WelcomeMessage' of type String Var DataTable As CTable; // Declares a variable named 'DataTable' of type CTable /* It is also possible to immediately assign a value when declaring variables */ Var WelcomeMessage As String = "Hello galaxy!"; // Declares a string and assigns it a value Var DataTable As CTable = New CTable(); // Declares a variable and assigns a newly created CTable to it Var InfoTable As New CTable(); // Also declares a variable and assigns a newly created CTable to it, but with less code
Conditional statements
It is possible to wrap code in conditional code blocks by using If, Else and ElseIf statements. If and ElseIf statements are followed by a boolean expression between round brackets. Code blocks following the If/Else/ElseIf statements have to be wrapped in curly brackets.
An example:
If (x <= 5) { WriteLine("Five or less."); } ElseIf (x <= 10) { WriteLine("More than five."); WriteLine("Ten or less."); } Else { WriteLine("More than ten."); }
Loops
There are a few kind of loop statements: For, For Each, While and Do...While loops. They work similar to For, While and Do...While loops in other programming languages. The For loop will increment a specified counter by a given step until a certain value is reached. The For Each loop iterates all elements of a CCollectionBase. The While and Do...While loops continue to loop while a boolean expression results in True. The difference between the While and Do...While loops is when the boolean expression is evaluated. While loops check the expression at the beginning of each iteration, Do...While loops check the expression at the end of each iteration. As a result, Do...While loops are always executed at least once.
An example:
/* This will print each number from 0 to 10. 0 and 10 included. */ Var i As Integer; For (i = 0 To 10) { WriteLine(i); } /* This will print the name of each ship in the SRS report of the ship with NCC 123456 */ Var MyShip As New CMyShip(125466); Var Current As CShip; For (Each Current In MyShip.SRS) { WriteLine(Current.Name); } /* This will print all even numbers from 0 to 10. 0 and 10 included. */ Var i As Integer; For (i = 0 To 10 Step 2) { WriteLine(i); } /* This will print each number from 0 to 10. 0 and 10 included. */ i = 0; While (i <= 10) { WriteLine(i++); // i++ increases i by one and returns the old value, see Operators for details. } /* This will also print each number from 0 to 11. 0 and 11 included! */ i = 0; Do { WriteLine(i++); } While(i <= 10)