Language
Variables
Variables are declared like so: var message = "hello";
Variables can also be declared without being given a value immediately.
var message;
message = "hello";
If statements
// if statements
var age = 17;
if (age >= 18) {
// perform action
} else if (age == 16) {
// perform alternate action
} else {
// perform some other action
}
While statements
var index = 0;
while (index < 10) {
index++;
// perform action
}
For statements
for (var i = 0; i < 10; i++) {
// perform action
}
Functions
function factorial(num) {
return num <= 1 ? 1 : num * factorial(num-1);
}
Anonymous functions
var printHello = function(name) {
println("hello " + name);
// no string templates yet
// println prints to a new line, like Java's System.out.println
}
printHello("world");
Classes
class Square {
// constructor
init(length) {
// fields
this.length = length;
}
// methods
area() {
return this.length ** 2;
}
perimeter() {
return 4 * this.length;
}
static numSides() {
return 4;
}
}
var smallSquare = Square(3);
println(smallSquare.area());
println(Square.numSides);
Inheritance
// inheritance
class Parent {
speak() {
println("Hello World!");
}
}
class Child extends Parent {
}
Child().speak() // Hello World!