Scala If-Else Statement Example Tutorial

Scala If-Else statement is a decision making statement which executes the conditional code if the statement is true otherwise executes the else block code. Decision making code is present in almost every application and every programming language, so we will go through with if-else Scala programming language syntax and usage in this post.

Scala If Statement

The if statement consists of Boolean expression followed by one or more statements.

The syntax for an if statement is


if(boolean_expression) {
statements
}

If the boolean expression is true then the statements in the if block execute gets executed else if the expression is false then the if block is terminated and the statements outside the if block gets executed.

Consider an example for if statement;


object Student {
def main(args:Array[String]) {
var studmarks = 75
if( studmarks > 65) {
studmarks += 20
println("Student passed the exam with distinction")
}
}
}

We created Student object with the main method. The variable studmarks is initialized with a value of 75. In the if block, if the marks is greater than 65 then the marks is incremented by 20 and the statement “student passed the exam with distinction” is printed. Since the marks is initialized to 75 which is greater than 65 the if loop statements are executed.

Run the above code by typing Student.main(null) and you will see output as Student passed the exam with distinction.

Till now I have used Scala shell to run programs, we can also save Scala class in a file with .scala extension and then compile and run it. Save the above code in the file named Student.scala and then run commands shown below to compile and execute it. After compiling you will also see some .class files created.


Pankaj:~ pankaj$ scalac Student.scala
Pankaj:~ pankaj$ scala Student
Student passed the exam with distinction
Pankaj:~ pankaj$

Scala If Else Statement

An if statement can be followed by an else statement which gets executed when the if condition is false.

The syntax of an if else statement is;


if(boolean_expression) {
statements
} else {
statements
}

If the boolean expression is true the if block statements are executed else if the if condition returns false the statements in the else block gets executed.

Consider an example for if else statement;


object Student {
def main(args:Array[String]) {
var marks = 55
if( marks > 65) {
marks += 20
println("Student passed the exam with distinction")
} else {
println("Student marks is less than 65")
}
}
}

We are creating an object Student with main method. The variable marks is initialized with a value of 55. The if statement returns false and hence the control is transferred to else block and the statements in the else block gets executed. Since the value of marks is 55 the if statement returns false and print the else block of statement that is “Student marks is less than 65”.

Run the above code by typing Student.main(null) and you will see output as Student marks is less than 65. Again you can save the code in file and compile/run it using scalac and scala commands.

Scala If Else-If Else statements

The if statement can be followed by multiple else-if else statements that can be used to test several conditions in a single if else if statement.


if(boolean_expression1) {
statements
} else if(boolean_expression2) {
statements
} else if(boolean_expression3) {
statements
} else if(boolean_expression4) {
statements
} else {
statement
}

If the boolean_expression1 returns true the statements following the if block will be executed else the control is transferred to else if statement and if the boolean_expression2 returns true the statements following this else if block is executed else the control is transferred to next else if block and if the boolean_expression 3 returns true the statements in this block gets executed else the control is transferred to next else if statement and if the boolean_expression 4 returns true the statements in this else if block gets executed. If none of the above boolean expressions are satisfied the else block of statement gets executed.

Consider an example for this.


object Student {
def main(args:Array[String]) {
var marks = 80
if(marks == 60 ) {
println("Grade is C")
} else if(marks == 70) {
println("Grade is B")
} else if(marks == 80) {
println("Grade is A")
} else if(marks == 90) {
println("Grade is A+")
} else {
println("Student is not performing well hence no grade is awarded")
}
}
}

Here we are creating a Student object with main method and the variable marks is initialized with a value of 80. There are multiple else if statements checking for the marks 60,70, 80 and 90 and if the boolean expressions are satisfied the else if statements are executed.
If none of the expressions are satisfied the else block statement shall be executed. Since the marks value is 90 the else if statement for this block is executed.

Run the above code by typing Student.main(null) in Scala shell and you will get output as Grade is A.

Scala Nested If Else statement

An if statement can be nested inside another if statement which is supported in Scala.

The syntax is


if(boolean_expression1) {
statements
if(boolean_expression2) {
statements
}
}

Consider an example;


object Student {
def main(args:Array[String]) {
var marks = 70
var id =10
if(id == 10 ) {
if( marks == 70) {
println("Student id is 10 and marks secured is 70")
}
}
}
}

Here we have created Student object by initializing marks and id with values 70 and 10 respectively. We are using two if statements first and if the id is 10 and marks is 70 then the statement gets executed.

Run the above code by typing Student.main(null) or by saving into a file and then compile/run it. You will get output as Student id is 10 and marks secured is 70.

That’s all for Scala decision making if-else statements example, we will look into more Scala features in coming posts.

By admin

Leave a Reply

%d bloggers like this: