Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
If-Else Statement | Conditional Statements and Loops
Introduction to Scala
course content

Зміст курсу

Introduction to Scala

Introduction to Scala

1. Getting Started
2. Variables and Data Types
3. Conditional Statements and Loops
4. Arrays
5. Strings

If-Else Statement

Relational Operators

Previously, we have discussed arithmetic operators, so let's now take a look at relational operators before delving deeper into conditional statements. Here is the list of all relational operators:

  • ==: Equal to operator checks whether two values are equal.
  • !=: Not equal to operator checks whether two values are not equal.
  • >: Greater than operator checks whether the left value is greater than the right value.
  • <: Less than operator checks whether the left value is less than the right value.
  • >=: Greater than or equal to operator checks whether the left value is greater than or equal to the right value.
  • <=: Less than or equal to operator checks whether the left value is less than or equal to the right value.

These operators compare two values and return a Boolean result which is either true or false based on the comparison.

If Statement

An if statement in Scala is used to test a condition and execute a block of code if the condition is true.

java

Main

copy
123
if (condition) { // Code to execute if condition is true }

Let's now take a look at the following flowchart to make things clear:

As you can see, if the condition results in true, the code block inside the if statement is executed. Here is an example of using such construct:

java

Main

copy
1234567891011
object Main { def main(args: Array[String]): Unit = { var currentSpeed = 40 val speedLimit = 50 currentSpeed *= 2 println("The current speed is " + currentSpeed) if (currentSpeed > speedLimit) { println("The speed limit is exceeded") } } }

What our small program does is simply determines if our current driving speed (currentSpeed) exceeds the speed limit (speedLimit). If it is indeed greater than the speed limit, than the respective message is printed.

Feel free to change the values of these variables and observe the results.

Else Statement

While if statements are suitable for executing a certain block of code based on a condition, we may often want to execute another code block if and only if the condition in the if statement does not hold.

Here is where the else statements come into play. An else statement can be added to an if statement to specify a block of code to be executed when the if condition is false.

Let's take a look at the following flowchart for such scenario:

As you can see, if the condition results in true, the code block inside the if statement is executed. Otherwise (condition is false), the code block inside the else statement is executed.

Here is an example of using if and else:

java

Main

copy
123456789101112131415
object Main { def main(args: Array[String]): Unit = { // Feel free to change the values of the variables and observe the result var currentSpeed = 40 val speedLimit = 50 currentSpeed += 5 println("The current speed is " + currentSpeed) if (currentSpeed > speedLimit) { println("The speed limit is exceeded") } else { println("The speed is wihin the limit") } } }

Once again, if our current speed (currentSpeed) is greater than (>) the speed limit (speedLimit), the respective message is printed. However, this time with the addition of the else statement we ensure that otherwise the other message is printed telling that the speed is within the limit.

What will be printed? (The print() method outputs without adding a newline at the end)

Виберіть правильну відповідь

Все було зрозуміло?

Секція 3. Розділ 1
We're sorry to hear that something went wrong. What happened?
some-alt