Scala

Scala If-Else

Programming decisions are comparable to those made in everyday life. When the specified condition is met, the decision-making code is run. These are also raised to as control flow statements at times. Scala employs the control statements to modify how the program is executed based on specific circumstances. These are employed to enable the execution flow to progress and diverge in response to modifications in the state of a program. We can refer to it as a decision-maker for our reasoning since the relevant portion of the script only runs if the prerequisite is met.

Therefore, when it comes to enforcing our logic in a practical setting, the decision-making statements “if and else” in object-oriented programming play a crucial role. Usually, there is no condition attached to the else statement; it only takes effect when the “if” expression is False. To summarize, the if-else statements are composed of Boolean expressions that operate on a True/False basis.

Example 1: Program of If Expression in Scala in Ubuntu 20.04

The “if” statement is the easiest decision-making statement. The block of code in this statement is only performed when the supplied condition is true. If the condition is false, the block of code is not executed. After examination, the condition, in this case, is either true or false. The “if” expression accepts the Boolean values. If the value of the expression evaluates to true, underneath it is executed.

We established our scala object as “Demo” for the code implementation. Inside the body of the “Demo”, the main method is built which declared the variable as “num” and the type set is int. We assigned a numeric value “10” as its data type is int to the variable “num”. After that, we have the “if” condition “num > 9”. As we have the numeric value “10” which has not fulfilled the condition, the println statement inside the “if” condition is executed.

The scala program is saved inside the “Example.scala” and complied with the object name “Demo” as given in the previous scala script. The statement of the “if” expression is printed in the following terminal as the condition is not fulfilled.

Example 2: Program of If-Else Expression in Scala in Ubuntu 20.04

A block of expression is performed if a condition evaluates to true. If it is untrue, it is not performed according to the “if” statement alone. If the statement is incorrect, what if we intend to do something else? Moving ahead is the else statement. By combining an if statement with an else statement, a block of statements can be executed when the expression is false.

We constructed the scala object as “New_Demo” with the object keyword. Within the object “New_Demo”, we invoked the main method definition. The main method has the implementation of the “if else” statement. First, we created the variable as “age” with the “var” modifier. The variable “age” is assigned and set with the number value “23” as its data type is “int”. Then, we have an if-else block where the if has the condition “age > 18”. The statement included within the “if ” section is evaluated when the age is greater than “18”. The statement within the else clause is executed if the age is less than “18”.

We have the value “23” which is above the value “18”. The condition is satisfied here so the if block statement “identity card required” is printed by the scala compiler as follows:

Example 3: Program of Nested If-Else Expression in Scala in Ubuntu 20.04

The term “nested” refers to an “if” expression that serves as the target of another if-else statement. When one if or else statement is contained inside of another, the if-else statement is said to be nested. We can nest the if-else statements inside of other if-else statements using the scala.

First, we built the object with the name “Nested”. After this, we established the main definition of the script inside that object “Nested”. We defined three variables with the help of the var keyword. As their type is set as “int”, we provided the numeric value to these variables, respectively. We then have the nested if-else conditions that must be satisfied. Initially, we have an if statement that has the condition “x > y”. Inside that “if” block, we have another “if” expression “x > z” that is also needed to be fulfilled. And we have the else statement which is executed when the if expression is not satisfied. Outside the “if” block, we have an else block which has another if else expression but with a different condition. The if block has the condition “y > z” and its statement executed when the condition is true. Otherwise, we have the else block for the execution upon the false condition.

As you know that from all the given x, y, and z values, z has the greater value of all. When the scala script is compiled, it generates the following else expression:

Example 4: Program of If-Else If Ladder in Scala in Ubuntu 20.04

The if expressions are carried out in reverse order. When one of the conditions governing the “if” is satisfied, the statement connected to that “if” is carried out and the remainder of the ladder is skipped. Finally, the else expression is carried out when all the conditions are untrue.

Begin with the creation of the abject. We have defined a scala object as “My_Demo”. The “My_Demo” has the main method definition. Then, we declared a “number” variable and initialized it with the “100”. After this, we have a ladder of the if-else. Inside which, we have the different conditions. One by one, the conditions are examined and executed where they are satisfied. If all the previous conditions do not fulfill the condition, the last else statement is printed as “Number Matched Not Found”.

As we have a number “100” that does not fulfill any of the previously defined condition, the else statement is executed in the terminal.

Conclusion

The decision-making control statements in IF-ELSE operate identically to any other scripting language in use, except for the slight syntax variations. Their decision-making outcome is dependent on the True and False values. They give us a method to manage the logic of our programs and aid in their development. In this article, we went over an overview of the Scala if-else statement, its various examples, and code implementation.

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.