ballerina programming language
Learn from examples
Updated for Balerina version 1.0.3
1. Program to output a string, integer, and floating point
exbl1.bal source code
# exbl1.bal example print string, integer, float
// exbl1.bal example print string, integer, float, boolean
// ballerina run exbl1.bal
import ballerina/io;
public function main (string... args) {
io:println("exbl1.bal running");
string hi = "Welcome to the Ballerina language";
io:println(hi);
int n = 3;
io:println("n=",n);
float x = 1.0;
float y = 1.0e+100; // no double
io:println("x=",x,", y=",y);
boolean bt = true;
boolean bf = false;
io:println("bt=",bt,", bf=",bf);
io:println("exbl1.bal ends");
} // end exbl1.bal
Output from execution:
exbl1.bal running
Welcome to the Ballerina language
n=3
x=1.0, y=1.0E100
bt=true, bf=false
exbl1.bal ends
2. commands to execute the source code
at a minimum, Windows, Linux, MacOSX.
Windows:
ballerina run exbl1.bal
Linux:
ballerina run exbl1.bal
MacOSX
ballerina run exbl1.bal
Sample Makefile:
3. You must be able to declare variables and arrays
and matrix of various types.
exbl3.bal source code
// exbl3.bal example declare and print variable, vector, matrix
// ballerina run exbl3.bal
import ballerina/io;
public function main (string... args) {
io:println("exbl3.bal running");
int n = 3;
n = n + 3;
io:println("n=",n);
float x = 1.0;
float y = x+2.0;
io:println("x=",x,", y=",y);
// This creates an `int` array of length 0.
int[] a = [];
io:println("lengthof a = ",a.length());
// This assigns an array literal.
int[] b = [1, 2, 3, 4, 5, 6, 7, 8];
io:println("b[0]) = ",b[0]);
io:println("b[7]) = ",b[7]);
io:println("lengthof b = ",b.length());
foreach int i in 0...7 { io:println("b[",i,"]=",b[i]); }
// Arrays are unbounded in length.
// They can grow up to any length based on the given index.
// In this example, the length of the array is 1000.
b[999] = 23;
io:println("b[999] = ",b[999]);
io:println("lengthof b = ",b.length());
//This initializes an array of int arrays.
int[][] iarray = [[1, 2, 3, 4], [10, 20, 30, 40], [5, 6, 7,8]];
io:println("lengthof iarray =",iarray.length());
io:println("lengthof iarray[0] = ",iarray[0].length());
// This initializes the outermost array with the implicit default value.
iarray = [];
int[] d = [9];
iarray[0] = d;
io:println("lengthof iarray =",iarray.length());
io:println("lengthof iarray[0] = ",iarray[0].length());
// This prints the first value of the two-dimensional array.
io:println("iarray[0][0] = ",iarray[0][0]);
// This creates a sealed `int` array of length 5.
int[5] e = [1, 2, 3, 4, 5];
io:println("lengthof e = ",e.length());
// This creates a sealed `int` array of length 5
int[5] f = [0, 0, 0, 0, 0];
io:println("lengthof f =",f.length());
// To infer the size of the sealed array from the array literal,
// following syntax is used.
int[!...] g = [1, 2, 3, 4];
io:println("lengthof g =",g.length());
io:println("arrays.bal finished");
io:println("exbl3.bal ends");
} // end exbl3.bal
Execution output:
exbl3.bal running
n=6
x=1.0, y=3.0
lengthof a = 0
b[0]) = 1
b[7]) = 8
lengthof b = 8
b[0]=1
b[1]=2
b[2]=3
b[3]=4
b[4]=5
b[5]=6
b[6]=7
b[7]=8
b[999] = 23
lengthof b = 1000
lengthof iarray =3
lengthof iarray[0] = 4
lengthof iarray =1
lengthof iarray[0] = 1
iarray[0][0] = 9
lengthof e = 5
lengthof f =5
lengthof g =4
arrays.bal finished
exbl3.bal ends
4. You need to be able to have loops, iteration statements
exbl4.bal source code
// exbl4.bal example loop, iteration
// ballerina run exbl4.bal
import ballerina/io;
public function main (string... args) {
io:println("exbl4.bal running");
int i;
int n = 3;
io:println("n=",n,++"[",i,"]=",a[i]);
}
} // end procedure prt_vec
function prt_mat(string name, float[][] a)
{
foreach int i in 0..7lt;a.length()
{
foreach int j in 0..<a[0].length()
{
io:println(name+"[",i,"][",j,"]=",a[i][j]);
}
}
} // end procedure prt_mat
public function main (string... args) {
io:println("exbl6.bal running");
int k;
int n = 2;
io:println("n= ",n);
int m = 3;
io:println("m= ",m);
k = add(n,m);
io:println("k=add(n,m) ",k);
float[] v1 = [1.0, 3.0, 5.0, 7.0];
prt_vec("v1", v1);
float[][] m1 = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
prt_mat("m1", m1);
io:println("exbl6.bal ends");
} // end exbl6.bal
Execution output:
exbl6.bal running
n= 2
m= 3
k=add(n,m) 5
v1[0]=1.0
v1[1]=3.0
v1[2]=5.0
v1[3]=7.0
m1[0][0]=1.0
m1[0][1]=2.0
m1[0][2]=3.0
m1[1][0]=4.0
m1[1][1]=5.0
m1[1][2]=6.0
exbl6.bal ends
7. You need to be able to read and write files in various formats.
exbl7.bal source code
// exbl7.bal example
// ballerina run exbl7.bal
import ballerina/io;
public function main (string... args) {
io:println("exbl7.bal running");
io:println("exbl7.bal ends");
} // end exbl7.bal
Execution output:
exbl7.bal running
exbl7.bal ends
8. You need to be able to use a number of files combined to
build a program. This may include packages, libraries,
operating system commands, header files, etc.
exbl8.bal source code
// exbl8.bal example
// ballerina run exbl8.bal
import ballerina/io;
public function main (string... args) {
io:println("exbl8.bal running");
io:println("exbl8.bal ends");
} // end exbl8.bal
Execution output:
Last updated 11/13/2019