Conditionals
Operators
Conditional statements evaluate to true or false only. The most common conditional operators are ==
(equal), !=
(not equal), >
(greater than), >=
(greater than or equal to), <
(less than), and <=
(less than or equal to). You can also define the operators as abbreviations: EQ, NEQ, GT, GTE, LT, and LTE
.
Some instructions return a true
or false
, so they're used in conditional statements, for example, IsArray
which is true
only when the variable is an "array". Structures have an instruction named structKeyExists()
or keyExists()
which returns true
if a key is present in a structure. Strings can also be used for conditional operations by checking the .length()
member function.
Also integers can be evaluated as true or false. In BoxLang, 0 (zero) is false and any other integers are true.
If, Else If, & Else
Why do we have conditional statements? Most often it's to control conditional instructions, especially if
/ else if
/ else
expressions. Let's write an example by adding a method to our PersonalChef.bx
class:
Try this example using 5, 7, 8 and 9 for the values of minutes.
When the minutes is 5, here is how the execution goes: Is it true that 5 is less than 7? Yes, it is, so print out the line
The water is not boiling yet.
.When the minutes is 7, it goes like this: Is it true that 7 is less than 7? No. Next, is it true that 7 is equal to 7? Yes, it is, so print out the line
It's just barely boiling
.When the minutes is 8, it goes like this: Is it true that 8 is less than 7? No. Next, is it true that 8 is equal to 7? No. Next, is it true that 8 is equal to 8? Yes, it is, so print out the line
It's boiling!.
Lastly, when total is 9, it goes:" Is it "true" that 9 is less than 7?
No. Next, is it true that 9 is equal to 7? No. Next, is it true that 9 is equal to 8? No. Since none of those are true, execute the else and print the line Hot! Hot! Hot!
.
An if
block has:
One
if
statement whose instructions are executed only if the statement is trueZero or more
else if
statements whose instructions are executed only if the statement is trueZero or one
else
statement whose instructions are executed if noif
norelse if
statements were true
Only one section of the if / else if / else
structure can have its instructions run. If the if is true, for instance, BoxLang will never look at the else if
. Once one block executes, that’s it.
Ternary Operator
The ternary operator is a compact way to do an if, else, else if
expression statements. It is very common in other languages and can be used for a more fluent expressive conditional expression.
The way it works is that the condition
is evaluated. If it is true, then the true statement executed; if it is false, then the false statement executes.
Please note that you can chain the trueStatement
and the falseStatement
into more tenrary operations. However, don't abuse it as they will look ugly and just be very complex to debug.
The output of the above statement will be..... true
of course!
Elvis Operator
Before Elvis we had isDefined(), structKeyExists()
and IF
statements to do these kind of evaluations. They work, but not very expressive or concise.
The Elvis operator is primarily used to assign the right default
for a variable or an expression Or it is a short-hand way to do parameterization. It will allow us to set a value if the variable is Null
or does not exist.
For instance,
If userName
does not exist or evaluates to null
then the default value of the myName
will be assigned the right part of the ?:
elvis operator -> Anonymous
Safe Navigation Operator
The safe navigation operator allows for you to navigate structures by not throwing the dreaded key not exists
exception but returning an undefined
or null
value. You can then combine that with the elvis operator and create nice chainable struct navigation. For example instead of doing things like:
You can do things like this:
The hook operator (?
) along with the dot operator (.
) is known as safe navigation operator(?.
). The safe navigation operator makes sure that if the variable used before the operator is not defined or java null
, then instead of throwing an error, the operator returns undefined
for that particular access.
Switch, Case, & Default
Another situation that involves conditional logic is when a single variable or expression that can have a variety of values and different statements or functions needed to be executed depending on what that value is. One way of handling this situation is with a switch / case / default
block.
Much like how the if
statement marks the start of an if
block and contains one or more else if
statements and perhaps one (and only one) else
statement, the switch
statement marks the start of a switch
block and can contain multiple case
statements and perhaps one (and only one) default
statement.
The main difference is that switch / case / default
can only evaluate the resulting value of a single variable or expression, while the if / else if / else
block lets you evaluate the true or false
result of different variables or expressions throughout the block.
Please note that you can create a body for the case
statements with curly braces. As best practice, do so for all case
and/or default
blocks
While Loops
The while( conditional )
expression allows you to execute a code block as many times as the conditional
expression evaluates to true. This is a great way to work with queues, stacks or just simple evaluations.
The ==
and =
Common Mistake
==
and =
Common MistakeThe #1 mistake people encounter when writing conditional statements is the difference between =
and ==
.
=
is an assignment. It means "take what's on the right side and stick it into whatever is on the left side" (or its telling not asking.)==
is a question. It means "is the thing on the right equal to the thing on the left" (or its asking not telling.)
Last updated