Introduction
To introduce the Xojo language, the main features of the language are summarised in this lesson. This lesson should allow the user to add simple code to Event Handlers .
Variables
Xojo is a strongly typed language: variables must be declared before they can be used. The syntax to declare a variable is as follows:
Var variable name As data type
Variable names
The following rules apply to naming a variable:
- Variable names must always start with a letter;
- Variable names may contain letters, numbers and some special characters (for instance underscore (_));
- Variable names in Xojo are case-insensitive (FirstName, firstname and FIRSTNAME refer to the same variable).
Use camel case (first letter is lowercase, subsequent words in the name start with a capital letter) to name variables (for instance firstName)
Data types
The following data types are frequently used:
Data type | Description | Example |
---|---|---|
Integer | Integer variables contain whole numbers | Var score As Integer score = 15 |
Double | Double variables contain decimal numbers | Var salary As Double salary = 4567.87 |
Currency | Fixed-decimal: mainly used for calculations with monetary data | Var bankBalance As Currency bankBalance = 123678.78 |
String | String variables contain a series of characters | Var myName As String myName = “John” |
Boolean | Boolean variables are limited to the values True or False | Var invoicePaid As Boolean invoicePaid = True |
Color | Color variables contain color values | Var highlight As Color highlight = Color.Red |
Mathematical operations
The following operators are used frequently:
Operator | Operation | Example |
---|---|---|
+ | Addition | 5 + 7 = 12 |
– | Subtraction | 12 – 5 = 7 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 22 / 4 = 5.5 |
^ | Exponentiation | 3 ^ 3 = 27 |
Complex mathematical expressions are evaluated using the following priority rules (evaluated from left to right):
- Parenthesis;
- Exponentiation;
- Multiplication and division;
- Addition and subtraction.
String operations
The + operator can be used to combine (concatenate) strings:
Operator | Operation | Example |
---|---|---|
+ | Concatenation of strings | “Napoleon ” + “Bonaparte” = “Napoleon Bonaparte” |
Arrays
An array is a collection of data for a specific data type. An element in an array is accessible through its index. The index of the first element is zero (0). Arrays can be defined as follows:
Var arrayName() As dataType Var arrayName(size1[,size2, size3...]) As dataType Var arrayName As dataType = Array(value1, value2...)
Examples: Var months() As String months.Add("January") Var months(12) As String months(0) = "January" Var months() As String = Array("January", "February", "March")
The following methods are very useful when editing arrays:
Method | Description | Example |
---|---|---|
Add | Adds a new element at the end of the array | months.Add(“January”) |
AddAt | Adds a new element at the specified index | months.AddAt(2,”March”) |
Count | Returns the number of elements in an array | numberElements = months.Count |
FirstIndex | Returns index of the first element | first = months.FirstIndex |
LastIndex | Returns index of the last element | last = months.LastIndex |
RemoveAll | Removes all elements | months.RemoveAll |
RemoveAt | Removes the element at the specified index | months.RemoveAt(2) |
Sort | Sorts the array in ascending order | months.Sort |
Assignment statements
The assignment operator in Xojo is =. A variable to the left of the = sign gets a value from the right side of the statement. The value on the right side of the = sign can be a constant, a variable or an expression.
variable = constant|variable|expression
Examples: age = 12 myAge = yourAge totalPrice = netPrice + salesTax
Selection statements
A selection statement will select one or more other statements based on the evaluation of a condition. There are two selection statements available in Xojo: If…Then…Else and Select…Case.
A conditional expression evaluates whether a relation between two values is True or False. In most cases, relational operators will be used to define a condition. Xojo defines the following relational operators:
Operator | Definition |
---|---|
= | Equal |
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
<> | Not equal |
Logical operators can be used to combine two conditional expressions into one logical expression:
Operator | Definition |
---|---|
And | AND (True if both conditions are True) |
Or | OR (True if any condition is True) |
Not | Not (inverts the result of a logical expression – If expression evaluates to True, Not returns False. If expression evaluates to False, Not returns True.) |
If…Then…Else
The If…Then…Else statement has the following syntax:
If conditional expression1 Then statement(s) ElseIf conditional expression2 Then statements(s) ElseIf conditional expression3 Then ... Else statement(s) End If
ElseIf and Else statements are optional. If the first conditional expression is True then the statements following the Then keyword are executed. If the first conditional expression is False then the ElseIf condition (if present) will be evaluated. If none of the conditional expressions are True then the Else statements (if present) will be executed.
Example: Var discountCode As Integer Var discount As Integer If discountCode = 1 Then discount = 5 ElseIf discountCode = 2 discount = 10 Else discount = 0 End If
Select…Case
The Select…Case statement tests a variable for a possible value. When the variable matches one of the values listed in a Case, the code following that Case keyword is executed. The Else sentence (no match was found) is optional.
The Select…Case statement has the following syntax:
Select Case variable Case value1 statement(s) Case value2 statement(s) Case value3 ... Else statement(s) End Select
Example: Var discountCode As Integer Var discount As Integer Select Case discountCode Case 1 discount = 5 Case 2 discount = 10 Else discount = 0 End Select
Iteration statements
Xojo has three statements to execute one or more lines of code multiple times: For…Next, While…Wend, Do…Loop.
For…Next
A For…Next statement is useful when you know how many times the iteration must run. The For…Next has the following syntax:
For variable = startValue To endValue Step stepValue statement(s) Next
The Step clause is optional. startValue, endValue and stepValue can be a constant or a variable.
Example: Var counterAs Integer Var sum As Integer = 0 For counter = 1 To 10 Step 1 sum = sum + counter Next
While…Wend
A While…Wend statement will run the lines of code between the While and the Wend sentences as long as the conditional expression evaluates to True. If the condition evaluates to False the execution will continue with the code after the Wend sentence. If the condition is already False at the start of the iteration then the iteration instructions will not be executed. The While…Wend statement has the following syntax:
While conditional expression statement(s) Wend
Example: Var counterAs Integer = 1 Var sum As Integer = 0 While counter <= 10 sum = sum + counter counter = counter + 1 Wend
Do…Loop
The Do…Loop statement will execute all lines of code between the Do and the Loop keywords until the condition evaluates to True. The conditional expression can either be at the beginning or at the end of the Do…Loop. The Do…Loop statement has the following syntax:
Do Until conditional expression statement(s) Loop Or Do statement(s) Loop Until conditional expression
Example: Var counterAs Integer = 1 Var sum As Integer = 0 Do Until counter > 10 sum = sum + counter counter = counter + 1 Loop