A general-purpose programming language with dynamic typing.
This section will walk you through the language features and standard library functions using example programs. You can copy the code in each example below and run it.
writeln("Saffron is awesome!");
The above program writes a string to the console. The writeln()
function is a standard library function
that prints a given value to the console.
var message;
message = "Saffron is awesome!";
writeln(message);
Variables are declared using the var
keyword.
Variables can also be initialized at the same time they are declared.
var message = "Saffron is awesome!";
Saffron supports three data types.
Number - is an 8-byte double precision floating point value.
String - is a sequence of Unicode characters.
Boolean - is either true
or false
.
var age = 30;
writeln(age);
var is_found = false;
writeln(is_found);
write("Enter your name: ");
var user = readln();
writeln("Namaste " + user + "!");
The readln()
function reads one line of text from the console. Also note the use of write
instead of writeln
. The former will NOT add a new-line character at the end.
write("Enter a number: ");
var str = readln();
var num = to_num(str);
writeln("I doubled your number: " + num * 2);
The to_num()
function is used to convert a string to a number.
Sometimes, a built-in function will throw an exception if any input is invalid. In the above example, if the user input is not a valid number, an exception will be thrown and the program will terminate.
These exceptions can be handled using the try...catch
statement.
write("Enter a number: ");
var inp = readln(); // Enter 'abcd' as input
writeln(to_num(inp) * 2); // Will throw exception and terminate
// BUT, using a try...catch block, we can handle the exception
try {
writeln(to_num(inp) * 2);
} catch (type, msg) {
if (type == "FORMAT_EXCEPTION") {
writeln(inp + " is not a proper number!");
}
}
A catch statement MUST have two and ONLY two parameters. The first parameter will have the type of exception and the second parameter will have the message describing the exception.
The documentation for each standard library function will list the type of exceptions thrown by that function.
write("Enter first number: ");
var n1 = to_num(readln());
write("Enter second number: ");
var n2 = to_num(readln());
if (n1 > n2) {
writeln("First number is greater than second number");
} else if (n1 < n2) {
writeln("First number is less than second number");
} else {
writeln("First number is equal to second number");
}
The if
keyword is used to execute a block of statements if a condition is true.
The else
keyword is used to execute a block of statements if the above condition is false.
You can also chain if-else statements to check a series of conditions as shown above.
var i = 1;
while (i <= 10) {
writeln(i);
i = i + 1;
}
The above program uses the while
loop to print numbers from 1 to 10.
Aside from the loop condition, you can control the execution of loops using the break
and
continue
statements.
var i = 1;
while (i <= 10) {
if (i % 2 == 0) {
i = i + 1;
continue;
}
writeln(i);
i = i + 1;
}
In the above program, we check whether the current value of i
is an even number
and skip the current iteration using the continue
statement.
Similarly, a break
statement can be used to exit a loop.
fun greet(user) {
return "Namaste " + user + "!";
}
write("Enter your name: ");
var user = readln();
writeln(greet(user));
In the above program, we define a function called greet which takes one parameter and returns a greeting.
A function is defined using the fun
keyword, followed by the name of the function.
The name of the function must be followed by a pair of open and close brackets ()
.
The function can optionally have arguments which are declared similar to variable declarations,
but without the var
keyword. These arguments must be specified inside the open and close brackets.
The body of the function must be specified in the form of a statement block.
fun print_something(msg) {
writeln(msg);
}
print_something("Saffron is awesome!");