In XBasic, you can assign values to variables using the following syntax:
datatype varname = value
Here's an example:
num x = 10
text name = "John"
XBasic supports the following data types:
- num: Integers and floating-point numbers.
- text: Strings of characters enclosed in double or single quotes.
- list: Comma separated collections of values. There must be atleast 2 values in a list
XBasic supports standard arithmetic operators for numeric operations:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Exponentiation (
^
)
Example:
num result = 10 * (5 + 3) / 2
XBasic provides comparison operators for comparing values:
- Equal to (
==
) - Not equal to (
!=
) - Less than (
<
) - Greater than (
>
) - Less than or equal to (
<=
) - Greater than or equal to (
>=
)
Example:
num x = 5
num y = 10
num is_greater = x > y
The IF
statement allows conditional execution of code:
IF condition THEN
// Code block executed if condition is true
ELSE
// Code block executed if condition is false
END
Example:
num x = 10
IF x > 5 THEN
PRINT "x is greater than 5"
ELSE
PRINT "x is not greater than 5"
END
The FOR
loop allows iterating over a range of values:
FOR variable = start_value TO end_value
// Code block executed for each iteration
NEXT
Example:
FOR i = 1 TO 5
print(i)
NEXT
The WHILE
loop repeats a block of code while a condition is true:
WHILE condition
// Code block executed while condition is true
END
Example:
num x = 1
WHILE x <= 5
print(x)
num x = x + 1
END
Functions in XBasic are defined using the FN
keyword:
FN functionName(param1, param2)
// Function body
RETURN result
END
Example:
FN greet(name)
RETURN name * 2
END
Functions are called by specifying the function name and passing arguments:
datatype result = functionName(arg1, arg2)
Example:
text val = greet("John")
Lists in XBasic are created using square brackets:
myList = [1, 2, 3, 4, 5]
XBasic provides various operations for manipulating lists, such as:
- Accessing elements by index
- Appending elements: You can do using the append method or using the + operator
- Removing elements: you can do using the minus operator
- Concatenating lists
Example:
myList[0] = 10 // Accessing element at index 0
myList.append(6) // Appending a new element
myList.remove(3) // Removing an element
newList = myList + [7, 8, 9] // Concatenating lists
XBasic provides various string operations for manipulating textual data. Here are some commonly used string operations:
Strings can be concatenated using the +
operator. This operation combines two or more strings into a single string.
Example:
text str1 = "Hello"
text str2 = "world"
text greeting = str1 + " " + str2 # Result: "Hello world"
The length of a string can be obtained using the len()
function. It returns the number of characters in the string.
Example:
text str = "Hello"
num length = len(str) # Result: 5
In XBasic, you can repeat a string multiple times by using the multiplication (*
) operator between a string and a numeric value. This operation duplicates the string the specified number of times.
text * num
text str = "Hello "
num repetitions = 3
text repeated_str = str * repetitions # Result: "Hello Hello Hello "
In the above example, the string "Hello "
is repeated three times, resulting in "Hello Hello Hello "
. This feature is useful when you need to generate repeated patterns or strings in your XBasic programs efficiently.