Similar to other programming languages, Ballerina has control logic statements, such as the C programming language, and these are very simple. The available control statements are as follows:
- while
- if ..else if .. else
- foreach
- Match
- Match expression
- Elvis
The following code shows an example using while that will run this loop twice and print an i value:
//while example
while (i < 2) { io:println(i); i = i + 1; }
//Output:
1
The following code is an example using the if ... else if ... else statement. It will print values according to the provided value of b. For example, if you set b=2, it will print b > 0:
// if ..else if..else example
if (b < 0) { io:println("b < 0"); } else if (b > 0) { io:println("b > 0"); } else { io:println("b == 0"); }
The following example will provide all the names in the boys' names list:
//foreach example
string[] boys = ["Shailender", "Shreyansh"];
foreach v in boys { io:println("boys: " + v); }
//Output:
Shailender
Shreyansh
There are many other statements and expressions and operators available in Ballerina, such as the Elvis operator and the match and match expression flow control statements.
There are many components that we should discuss when describing Ballerina, including its runtime environment, how it can deploy code on most of the latest orchestration tools, such as Kubernetes and Docker, and its life cycle, from writing the source code to production and deployment.