Conditionals
A conditional statement is programming construct that executes different code depending on whether a given condition is true or false. It enables control over the flow of your program by making decisions based on boolean expressoins.
In Cambo, there are 3 main statements built into the language:
- if statement
- match statement.
- expression-based statement.
If statement
An if statement is a control strucuture that executes a line or a block of code when a specified condition evaluates to true.
if
if(condition){
# code...
}
Example:
int age = 18;
if(age == 18){
print("you are 18.");
}
In the code above, you are 18. will be shown, as the condition
age == 18 is true in this case.
if else
The else clause is optional, can be included or ommited depending entirely on your implementation needs.
if(condition){
# code ...
} else {
# code ...
}
Example:
int age = 15;
if(age >= 18){
print("you're eligible.");
} else {
print("you're not eligible.");
}
In this case age is assigned to be 15, so you're not eligible. will be printed into the terminal.
else if
In some cases, multiple conditions, more than two, need to be evaulated in order to control the flow of a program effectively. This can be achieved by chaining the else if statement, as shown below:
if(condition){
# code ...
} else if(condition){
# code ...
} else {
# fall back code...
}
Example:
if(age > 18){
print("you're over 18.");
} else if(age < 18){
print("you're below 18.");
} else {
print("you're 18.");
}
Here we can check if age is above or below 18. Also, you can clearly see that we don't need to check if age is 18 or not, the else takes care of this for us already.
if(condition)
# a single-line code;
else
# a single-line fall back code;
Match statement
"A match is a conditional statement that evaluates an expression, compares the result against multiple case values, and executes the corresponding block of code for the first match found."
match(expression){
case value:
# code ...
case value:
# code ...
case value:
# code ...
otherwise:
# fall back code ...
}
Example:
# assume `day` is a variable of string type
match(day){
case "mon":
print("today is monday.");
case "tue":
print("today is tuesday.");
case "wed":
print("today is wednesday.");
case "thu":
print("today is thurday.");
case "fri":
print("today is friday.");
case "sat":
print("today is saturday.");
case "sun":
print("today is sunday.");
otherwise:
print("ehh?");
}
If you have one same block of code for multiple cases, you can use | to indicate one or more alternatives. For example:
match(day){
case "mon" | "tue" | "wed" | "thu" | "fri":
print("today is a weekday.");
case "sat" | "sun":
print("today is a weekend.");
otherwise:
print("ehh?");
}
Using break
break is a control-flow keyword that terminates the current execution path and immediately exits the match statement.
match(day){
case "mon" | "tue" | "wed" | "thu" | "fri":
print("today is a weekday.");
break;
print("This won't get executed!");
case "sat" | "sun":
print("today is a weekend.");
}