0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Expressions over Statements

Everything returns a value.

Expressions over Statements is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Everything Is an Expression

In many languages, if and blocks are statements that do something but produce no value. In Scala they are expressions that evaluate to a value.

This shift is at the heart of functional programming in Scala.

val max = if (3 > 2) 3 else 2

if as an Expression

A Scala if/else returns the value of the branch that runs. You can assign that result directly to a val.

No need for a mutable variable that you set inside the branches.

object Main extends App {
  val temp = 18
  val label = if (temp > 20) "warm" else "cool"
  println(label)
}

Blocks Return Their Last Line

A block { ... } is itself an expression. Its value is the value of the last expression inside it.

Here the block evaluates to 30, which becomes the value of result.

object Main extends App {
  val result = {
    val a = 10
    val b = 20
    a + b
  }
  println(result)
}

No Explicit return Needed

Because a method body is an expression, its value is simply the last expression evaluated. The return keyword is rarely used.

Both methods below behave the same, but the idiomatic one omits return.

def double(x: Int) = x * 2
// instead of: def double(x: Int) = { return x * 2 }

match Is an Expression Too

Pattern matching with match also yields a value, making it perfect for mapping inputs to results.

Each case arm produces the value for that branch.

object Main extends App {
  val day = 3
  val name = day match {
    case 1 => "Mon"
    case 3 => "Wed"
    case _ => "Other"
  }
  println(name)
}

Expressions Compose

Because expressions return values, you can nest and combine them freely.

An if can be the argument to a function, or part of a larger calculation, without temporary variables.

object Main extends App {
  val n = 7
  println("sign: " + (if (n >= 0) "+" else "-"))
}

Statements Still Exist

Not everything produces a useful value. Operations done only for their side effects, like println or an assignment, evaluate to Unit.

These are Scala's statement-like expressions.

val x: Unit = println("hello") // value is ()

Avoid var With Expressions

The expression style lets you replace mutable var patterns. Instead of declaring a var and assigning it in branches, assign the whole if to a val.

object Main extends App {
  val score = 85
  val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"
  println(grade)
}

for Yields Values

A for with yield is an expression that builds a new collection, rather than just looping for side effects.

This turns iteration into a value-producing transformation.

object Main extends App {
  val squares = for (i <- 1 to 4) yield i * i
  println(squares)
}

Why It Matters for Backend Code

Expression-oriented code tends to be shorter, has fewer moving parts, and is easier to test.

  • Fewer mutable variables means fewer bugs.
  • Results flow naturally from inputs.
  • It composes well with Scala's functional libraries.

Statement Style vs Expression Style

Compare the two mindsets. The statement style mutates a variable; the expression style returns a value directly.

Scala lets you do both, but prefers the expression style.

var s = ""            // statement style
if (true) s = "yes" else s = "no"

val e = if (true) "yes" else "no" // expression style

Quick Check

What does an if/else evaluate to in Scala?

Recap

Scala favors expressions that produce values over statements that only mutate state.

  • if, match, blocks, and for/yield all return values.
  • Method bodies return their last expression; return is rarely needed.
  • Side-effecting code returns Unit.
  • This style reduces var usage and bugs.

Frequently asked questions

Is the “Expressions over Statements” lesson free?

Yes — the full text of “Expressions over Statements” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Expressions over Statements”?

Everything returns a value. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Expressions over Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. val vs var
  2. Basic Types and Literals
  3. Type Inference
  4. Expressions over Statements
← Back to Scala for Backend Engineering & Functional Programming