Outdated Notice
A basic Scala if
statement looks like this:
if (a == b) doSomething()
You can also write that statement like this:
if (a == b) {
doSomething()
}
The if
/else
construct looks like this:
if (a == b) {
doSomething()
} else {
doSomethingElse()
}
The complete Scala if/else-if/else expression looks like this:
if (test1) {
doX()
} else if (test2) {
doY()
} else {
doZ()
}
if
expressions always return a result
A great thing about the Scala if
construct is that it always returns a result. You can ignore the result as we did in the previous examples, but a more common approach — especially in functional programming — is to assign the result to a variable:
val minValue = if (a < b) a else b
This is cool for several reasons, including the fact that it means that Scala doesn’t require a special “ternary” operator.
Aside: Expression-oriented programming
As a brief note about programming in general, when every expression you write returns a value, that style is referred to as expression-oriented programming, or EOP. This is an example of an expression:
val minValue = if (a < b) a else b
Conversely, lines of code that don’t return values are called statements, and they are used for their side-effects. For example, these lines of code don’t return values, so they are used for their side effects:
if (a == b) doSomething()
println("Hello")
The first example runs the doSomething
method as a side effect when a
is equal to b
. The second example is used for the side effect of writing a string to STDOUT. As you learn more about Scala you’ll find yourself writing more expressions and fewer statements. The differences between expressions and statements will also become more apparent.
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next