Start Coding

Variables

Variables are used to store data. Variables can be declared with the let keyword.

let variable = 12;

In this example, the variable variable is declared with the value 12.

BBAP is a statically typed language, which means that the type of the variable is determined at compile time. In most cases, the type of the variable is determined by the value of the variable. In the example above, the type of the variable is int.

The type of the variable can also be specified manually. This is done by adding a colon and the type after the variable name.

let variable: int = 12;

In this example, the type of the variable is specified manually. This is not necessary, but can be useful in some cases.

Types

There are multiple basic types. The basic types are:

More information about the types can be found here for numbers and here for text.

let intVariable: int = 12; let longVariable: long = 1234567890123456789; let floatVariable: float = 12.34; let doubleVariable: double = 19.123456786789; let stringVariable = "Hello World!"; let boolVariable = true;

Calculations

Calculations can be done with the basic operators. The basic operators are:

let i = 1 + 2; // 3 let j = 3 - 2; // 1 let k = 2 * 3; // 6 let l = 6 / 2; // 3 let m = 5 % 2; // 1

The % operator returns the remainder of a division. For example, 5 % 2 is 1, because 2 goes into 5 two times, with a remainder of 1.

The order of operations can be changed with parentheses.

Additionally, there are the increment and decrement operators. The increment operator is ++ and the decrement operator is --.

let i = 0; i++; // 1 i--; // 0

If

The if keyword is used to execute code only if a condition is true.

let i = 0; if (i == 0) { printLine("Hello World!"); }

In this example, the code inside the if block will only be executed if i is equal to 0.

The condition is specified inside the parentheses after the if keyword. The parentheses are optional, but recommended.

Comparison Operators

The condition can be any expression that returns a boolean value. For example, the condition can be a comparison, like in the example above. The other comparison operators are:

Logical Operators

Logical operators can be used to combine multiple conditions.

let i = 0; if (i > 5 && i < 12) { printLine("Hello World!"); }

In this example, the code inside the if block will only be executed if i is larger than 5 and smaller than 12.

The logical operators are:

The ! operator can be used to negate a condition. For example, !(i == 0) is true if i is not equal to 0.

Loops

Loops are used to execute code multiple times.

While

The while keyword is used to execute code while a condition is true.

let i = 0; while (i < 10) { printLine(i); i++; }

In this example, the code inside the while block will be executed while i is smaller than 10.

For Loops

For loops are used to execute code a specific number of times.

for (let i = 0; i < 10; i++;) { printLine(i); }

In this example, the code inside the for block will be executed 10 times. This means it is the same as the while loop above.

The for loop has three parts:

Functions

Functions are used to execute code multiple times from different places.

func example() { printLine("Hello World!"); } example(); // Hello World!

In this example, the function example is defined and then called.

Parameters

Functions can have parameters. Parameters are used to pass data to the function.

func example(parameter: int) { printLine(parameter); } example(12); // 12 example(1); // 1

In this example, the function example has one parameter. The parameter is called parameter and is of type int.

Return Values

Functions can return values. The return value is the value that the function returns.

func example(parameter: int): int { return parameter * 2; } let i = example(12); // 24 let j = example(1); // 2

In this example, the function example returns the parameter multiplied by 2.

A function call also return multiple values.

func example(parameter: int): (int, int) { let multiplied = parameter * 2; return (parameter, multiplied); } let (i, j) = example(12); // i = 12, j = 24 let (k, l) = example(1); // k = 1, l = 2

In this example, the function example returns the parameter and the parameter multiplied by 2.

Comments

Comments are used to add notes to the code. Comments are ignored by the compiler. You maybe noticed that the comments in this tutorial previously.

There are two types of comments. Single line comments and multi line comments. Single line comments start with // and end at the end of the line. Multi line comments start with /* and end with */.

// This is a single line comment /* This is a multi line comment. It can span multiple lines. */

Structs

Structs are a way to store multiple values in one variable. For example, if you want to store a person's name and age, you can do it like this:

struct Person { name: string; age: int; }

Now you can create a variable of the type Person and store the name and age in it.

let person = new Person{ name: "John", age: 12 };

You can access the values of the struct with a dot:

printLine(person.name); // John printLine(person.age); // 12

To learn more, go to structs.