Updated April 6, 2023
Introduction to Scala if else
If else statements are the block of code which decides that the specific piece of code should be executed or not based on the value provided. It is similar like java and other languages available. We can say it is a decision maker for our logic and the part of code will only be executed if the condition is satisfied. So if and else are the decision making statement n object oriented programming which are very important when it comes to apply our logic in practice scenario.
Syntax:
if(your condition here){
// here the piece of code you want o execute
}else{
// code to be executed.
}
If the condition you have provided is computed as TRUE than the code will execute if not then it will not enter into the block.
In the example above if block will only be executed if the result compute out to be TRUE. In programming language this IF statement s followed by the ELSE statement as well. If you want to execute some code if the result is not TRUE then this else lock will be executed. This is not mandatory to provide else statement with IF block, it’s optional or we can say based on the requirement or logic we want to implement.
Flowchart of Scala if else
Below you can find the flowchart for IF and ELSE statement. First it will enter to code and check the condition provided into the IF statement. In programming language IF and ELSE statement depends upon the Boolean value i.e. TRUE and FALSE.
So after entering it will calculate the if condition result if it results out to be trues then the code inside IF block will be come into consideration. If it calculates False then the code other code will be executed. But if we have provided else statement, then code inside it will be executed.
In Scala we have various kind of IF statement;
- IF-ELSE Statement
- Nested IF-ELSE
- IF-ELSE-IF-ELSE
But for now, we will focus more on the IF ELSE Statement. In order to use IF ELSE we need to understand the IF statement first because the ELSE statement has to contain IF, but IF can execute independently.
1. IF-ELSE Statement: In this case we will have If statement followed by ELSE statement. If the condition provided is true then if the block will be executed, if false then Else block will be executed. Else will always execute in the case of false or when the condition is not satisfied.
syntax:
if(){
// if logic
}else{
// else logic
}
Example for beginners:
Code:
object Main extends App{
// Your code here!
vari = 10
if(i == 10){
println("only executed when the condition is TRUE :: Equal")
}else{
println("not equal")
}
}
2. Nested IF-ElSE: In this we have if-else statement inside if or an else statement depends upon the logic. We are doing nesting of statement inside an already present statement.
Syntax:
if(condition){
// if true (nesting)
if(){
}else{
}
}else{
// if false (nesting)
if(){
}else{
}
}
3. IF-ELSE-IF-ELSE: In this case we provide multiple if conditions. If any of the conditions is true then the rest condition will be skipped and the program will execute normally.
Syntax:
if(condition_1){
}else if(condition_2){
}else if(condition_3){
}else{
}
How Does if else Statement Works in Scala?
In Scala also these are the decision-making statements. Which allows us to control our flow of the program by the condition provided? They are depending upon the Boolean values to execute code. If true then a particular block of code will be executed. In this else block is not always mandatory it just depends upon the business requirement. Keep in mind that we need to implement IF statement in order to work with IF ELSE. This is mandate.
They are responsible to control the flow of the program.
Examples to Implement of Scala if else
Below are the examples of Scala if else:
Example #1 – IF-ELSE Statement
This is a simple IF ELSE statement Based on the TRUE and FALSE value.
Code:
object Main extends App{
//your code
println("Example to show if else statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 100
if(x > y){
println("If block executed.")
println("x is greater." + "x is >> " + x + " , y is >> " + y )
}else{
println("else block executed.")
println("y is greater. " + "x is >> " + x + " , y is >> " + y )
}
}
Output:
Example #2 – Nested IF Else Statement
It contains the nesting of statements.
Code:
object Main extends App{
println("Example to show nested if else statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 100
if(x > y){
println("If block executed.")
var z = 150
println("nested if statment!")
if(x > z ){
println("------------------------------------------------------")
println("x is greater than z.")
println("x is >> "+ x +" ,z is >>" +z)
println("------------------------------------------------------")
}else{
println("------------------------------------------------------")
println("x is not greater than z.")
println("x is >> "+ x +" ,z is >>" +z)
println("------------------------------------------------------")
}
}else{
println("else block executed.")
var z = 150
if(y > z ){
println("------------------------------------------------------")
println("y is greater than z.")
println("y is >> "+ y +" ,z is >>" +z)
println("------------------------------------------------------")
}else{
println("------------------------------------------------------")
println("y is not greater than z.")
println("y is >> "+ y +" ,z is >>" +z)
println("------------------------------------------------------")
}
}
}
Output:
Example #3 – IF ELSE IF Ladder
It contains the ladder of IF-ELSE statements multiple times.
Code:
object Main extends App{
println("Example to show if else if ladder statement flow.")
println("------------------------------------------------------")
var x = 90
var y = 90
if(x > y){
println("X is greater than Y.")
}else if(x == y ) {
println("X is equal to Y.")
}else{
println("Y is greater than X.")
}
}
Output:
Conclusion
IF ELSE are decision making control statement and they work similarly to any other language available only the syntax can vary from one to another. Their result depends upon the True and False value in order to make a decision. They provide us a way to control our program flow and help building logic.
Recommended Articles
This is a guide to Scala if else. Here we discuss a brief overview of Scala if else and its different examples along with code Implementation. You can also go through our other suggested articles to learn more –