Scala Boolean

Boolean expressions are very well-known and widely used in different programming languages to perform different comparisons among different values. A Boolean expression can have only two values, i.e., true or false. You can modify your output messages or result according to their two values. Within the Scala programming language, you can use Boolean expressions in different statements with different inputs to check for certain comparisons. This guide will explain how to use Boolean expressions in Scala programming.

Example 01

We will start this guide with our first and basic example of Scala programming that will utilize the Boolean expressions to be checked. Use “Ctrl+Alt+T” followed by the “scala” instruction on a terminal to open the terminal and launch the Scala shell on it as displayed. We started our example by initializing a variable “a” with integer value 14.

After that, we tried the “if” statement in the second query line with a Boolean expression in its brackets. The statement will be checking whether the value of variable “a” is greater than 10 or not. If so, its println() statement part would be executed and display “a is greater than 10”. The output shows that the value “14” of the variable “a” is greater than 10 after the evaluation of the Boolean expression within the “if” statement.

We tried the if-else statement at the last query line to check another Boolean expression. This time, we have provided a Boolean value “true” to the “if” part and println would be displaying “Boolean” on getting the Boolean value. The else part would display “Not Boolean” after finding any non-Boolean value in the Boolean expression of the “if” part. The output shows “Boolean” because the value “true” is Boolean itself.

The same Boolean expressions of the if-else statement can be utilized to compare more than two variable values. Thus, we have initialized two new variables, “y” and “z”, with 14 and 6 using the keyword “var” at the separate query line of the Scala command line utility. The output shows that both the Integer values have been initialized successfully.

After that, we tried the “if-else” statement in the third query line to evaluate and check the Boolean expression mentioned in the brackets of the “if” part. This expression would be taking both variables “y” and “z”, calculating their sum via “y+z” and checking whether the calculated sum is greater than or equal to 20 or not. On checking this Boolean expression, if the compiler shows “true” in return, the “println” function statement of the “if” part would display “Sum is 20”. Otherwise, the “else” part would execute its “println” function statement and display that the “Sum is not 20”. On completion of this if-else statement, we have “Sum is 20” as output.

Example 02

While working with an if-else statement, you may have tried the compound if-else statement by combining more than 1 if-else expression in a single statement. It is very useful when you want to check more than 1 Boolean expression of your choice. So, we have initialized variable “v” with value 5 and created a compound if-else statement to check if “r” is less than 10 or greater than 10, or equal to 10. The println() statements have been utilized according to each Boolean expression. We have the output “Value is Small” out of this method.

The nested if statement can be utilized in Scala to check more than 1 Boolean expression at a time. So, we have initialized two variables, “a” and “b”, with a value “13” and “6” as per the first two queries on the Scala command line. Then we used the Boolean expression in the if part of the statement to check if “a” is equal to 13 or not. If so, it will be using another “if” Boolean expression in the outer “if” part to check whether “b” is equal to 6 or not.

If the inner “if” part returns true after evaluating its Boolean expression, it will execute the println() function statement to display “True”, and the nested “if” statement would be completed. For this program, the output is “True”, as shown below.

Example 03

Within this example, we are utilizing the 16-bit signed integer via the “short” function to check the Boolean expression. In this function, we have initialized variable “v” with a Boolean expression checking whether the short value 199 is equal to integer value 199 or not. The output shows that it’s true, i.e., both short and integer value is the same.

We have tried the same Boolean expression on different values, i.e., 120 and 122, and the result is false.

Example 04

Let’s look at the last example of this guide to evaluate the Boolean expression in an if-else statement. So, we have started the main() function definition with the initialization of 2 Boolean variables in quite different ways. Although both variables are Boolean, we can display our messages as we want.

So, we have started the “if” statement with “x” as its expression. If “x” is true, it will execute the internal parts; otherwise, just quit the program. The println() would display that “x” is Boolean, and another “if” part would check for variable “y” same as we have checked for “x”. The else part would be displaying that “y” is Boolean, while the “else” part of the outer “if” would be executed after that to display that “x” is not Boolean. All the if-else statement brackets, along with the main function and object brackets, are closed here.

On compilation and execution, we have found that both are Boolean.


That’s how you can utilize the Boolean expressions in different if-else statements with different methods, i.e., simple if-else, compound if-else, or nested if-else. We have tried many examples of Scala using the Scala command line and Scala files through compilation and execution. All the examples are very brief and easy to understand for new users of Scala. Don’t worry, and start implementing the mentioned illustrations at your end.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.