Control logic expression

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:

  1. while
  2. if ..else if .. else
  3. foreach
  4. Match
  5. Match expression
  6. 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.

All referenced examples can be found by going to the following link: https://ballerina.io/learn/by-example/.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.29.145