Scala If Else statements are used for decision making in code by evaluating one or more conditions. This article will discuss different type of If Else statements in Scala programming.
Different types of Scala If Else Statements
Scala If Else statements can be a simple If, If Else, If..Else If..Else or Nested If Else statement. Let us discuss syntax and example of each type.
1. Scala If statement
If statement is used to validate input conditions. Statements or logic inside the If block executes only when input conditions are true.
Syntax of If statement
if (Input Condition)
Statement / Logic to be executed when condition is true;
Example
// file name : ifelse1.scala
println(“Please Enter your current age”);
/* readInt method reads input
from console */
var curr_age=scala.io.StdIn.readInt();
if(curr_age > 20)
println(“you are older than 20”)
In above example, we have to display message only when current age is more than 20, so here If statement is used. Now let us load file in REPL.
// loading ifelse1.scala
scala> :load ifelse1.scala
Loading ifelse1.scala…
Please Enter your current age
curr_age: Int = 30
// output
you are older than 20
2. Scala IF Else statement
Scala also allows an optional Else with If statement. Else Block executes only when test condition for If Block is false.
Syntax of If Else statement
if (Input Condition)
Statement / Logic to be executed when condition is true;
else
Statement / Logic to be executed when condition is false;
Example
//file name:ifelse2.scala
println(“Please Enter your current age”);
var curr_age=scala.io.StdIn.readInt();
if (curr_age >= 18)
{
println(“Congratulations !!! You can vote”);
println(“Don’t forget to carry Voter ID with you at polling Booth”);
} else
{
println(“Sorry you have to wait “+ (18-curr_age)+” more year to vote”);
println(“Please come when you are 18 years”);
}
In above example, we have to verify following two conditions to check eligibility:
- age greater than or equal to 18
- age less than 18
So here we have used If Else. Now let us load file to verify code.
//loading ifelse2.scala
scala> :load ifelse2.scala
Loading ifelse2.scala…
Please Enter your current age
curr_age: Int = 9
// output if age is less than 18
Sorry you have to wait 9 more year to vote
Please come when you are 18 years
//loading ifelse2.scala
scala> :load ifelse2.scala
Loading ifelse2.scala…
Please Enter your current age
curr_age: Int = 19
//output if age is above 18
Congratulations !!! You can vote
Don’t forget to carry Voter ID with you at polling Booth
3. Scala If..Else If..Else statement
Scala If Else statement can be extended to verify several conditions by using If..Else If..Else ladder. The syntax starts with a If Block, one or more Else If Block in the middle and a Else Block in last.
Syntax of If..Else If..Else Statement
if (Input Condition 1)
{
Logic/statements that will execute when condition 1 is valid;
} else if (Input Condition 2)
{
Logic/statements that will execute when condition 2 is valid and
condition 1 is not valid;
} else if (Input Condition 3)
{
Logic/statements that will execute when condition 1 and 2 are not valid and 3 is valid;
}. . . . else
{
Logic/statements that will execute when all the conditions are not valid;
}
Example
//file name:ifelse3.scala
//Assuming there are 5 subjects and total marks are 500
println(“Please Enter your Total Marks”);
val total=scala.io.StdIn.readInt();
if (total >= 450)
{
println(“Congratulations!!!!”);
println(“You can apply for 100% Scholarship”);
} else if (total >= 400)
{
println(“Congratulations!!!!”);
println(“You can apply for 50% Scholarship”);
} else if (total >= 350)
{
println(“Congratulations!!!!”);
println(“You can apply for 10% Scholarship”);
} else
{
println(“Sorry”);
println(“You cannot apply for Scholarship”);
}
In above example, we have to verify 4 different conditions to check Scholarship percentage:
- total marks greater than 450
- total marks greater or equal to 400 but less than 450
- total marks greater or equal to 350 but less than 400
- total marks less than 350
So here we have used Else If in addition to If and Else Block.
// loading ifelse3.scala
scala> :load ifelse3.scala
Loading ifelse3.scala…
Please Enter your Total Marks
total: Int = 450
//output if marks are greater or equal to 450
Congratulations!!!!
You can apply for 100% Scholarship
// loading ifelse3.scala
scala> :load ifelse3.scala
Loading ifelse3.scala…
Please Enter your Total Marks
total: Int = 410
// output if marks are greater or equal to 400 but less than 450
Congratulations!!!!
You can apply for 50% Scholarship
// loading ifelse3.scala
scala> :load ifelse3.scala
Loading ifelse3.scala…
Please Enter your Total Marks
total: Int = 354
//output if marks greater or equal to 350 but less than 400
Congratulations!!!!
You can apply for 10% Scholarship
// loading ifelse3.scala
scala> :load ifelse3.scala
Loading ifelse3.scala…
Please Enter your Total Marks
total: Int = 349
// output if marks less than 350
Sorry
You cannot apply for Scholarship
4. Scala Nested If Else statement
When Scala If Else statements are written inside an If Block , Else If or Else Block. These type of statements are called Nested If Else.
Example
//file name: ifelse4.scala
println(“Please Enter your current age “);
val current_age=scala.io.StdIn.readInt();
if(current_age < 18)
{
println(“Sorry. You have to be at least 18”);
} else if (current_age >= 18 && current_age <= 60 )
{ // Nested If Else inside Else If Block
println(“Congratulation!!! You are eligible to work”);
if(current_age >=18 && current_age < 40)
{
println(“You have to work in shifts”);
} else
{
println(“You have work in day shifts only”);
}
} else
{
println(“Sorry. Maximum age for this work is 60”);
}
In the above example, we have to verify following 3 conditions:
- age less than 18
- age greater than or equal to 18 and less than or equal to 60
- age greater than or equal to 18 and less than 40
- age greater than or equal 40
- age more than 60
If a candidate satisfy condition 2 then we have to verify 2 more conditions, therefore Nested If Else is used in Else If Block.
// loading ifelse4.scala
scala> :load ifelse4.scala
Loading ifelse4.scala…
Please Enter your current age
current_age: Int = 12
// output when age is less than 18
Sorry. You have to be at least 18
// loading ifelse4.scala
scala> :load ifelse4.scala
Loading ifelse4.scala…
Please Enter your current age
current_age: Int = 20
// output when age is less than 40
Congratulation!!! You are eligible to work
You have to work in shifts
// loading ifelse4.scala
scala> :load ifelse4.scala
Loading ifelse4.scala…
Please Enter your current age
current_age: Int = 40
// output when age is greater or equal to 40
Congratulation!!! You are eligible to work
You have work in day shifts only
// loading ifelse4.scala
scala> :load ifelse4.scala
Loading ifelse4.scala…
Please Enter your current age
current_age: Int = 61
// output when age is above 60
Sorry. Maximum age for this work is 60
Conclusion
We have discussed different types of If Else statements in Scala programming language and discussed examples to illustrate each statement.
I hope this article has provided you an overview of If Else Statements.
Recent Comments