0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Expressions over Statements

Everything is an expression.

Expressions over Statements is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 3 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.

Expressions vs Statements

A statement does something but returns nothing useful. An expression evaluates to a value.

In Scala, almost everything is an expression. This is a core idea that shapes how you write code.

if Is an Expression

In many languages if is a statement. In Scala, if/else returns a value, so you can assign it directly to a val.

No need for a separate variable assigned in each branch.

object Main {
  def main(args: Array[String]): Unit = {
    val temp = 30
    val label = if (temp > 25) "hot" else "mild"
    println(label)
  }
}

Blocks Are Expressions

A block { ... } is an expression too. Its value is the value of its last line.

So you can compute intermediate values, then end with the result.

object Main {
  def main(args: Array[String]): Unit = {
    val result = {
      val a = 2
      val b = 3
      a * b
    }
    println(result)
  }
}

match Is an Expression

Pattern matching with match also returns a value. Each branch produces a result, and the whole expression yields the matched branch's value.

object Main {
  def main(args: Array[String]): Unit = {
    val day = 3
    val name = day match {
      case 1 => "Mon"
      case 2 => "Tue"
      case 3 => "Wed"
      case _ => "Other"
    }
    println(name)
  }
}

The Unit Type

Some expressions exist only for their side effect, like println. They return Unit, written ().

Unit is similar to void in other languages but is still a real value.

object Main {
  def main(args: Array[String]): Unit = {
    val nothing: Unit = println("side effect")
    println(nothing)
  }
}

Returning From Functions

Because the body of a function is an expression, the last expression is its return value. You rarely need the return keyword.

Here the function body is a single expression.

object Main {
  def max(a: Int, b: Int): Int = if (a > b) a else b
  def main(args: Array[String]): Unit = {
    println(max(7, 4))
  }
}

Expression-Oriented Functions

A function can be a block expression whose last line is the result. This keeps logic compact and avoids mutable temporaries.

object Main {
  def discount(price: Double): Double = {
    val rate = if (price > 100) 0.2 else 0.1
    price * (1 - rate)
  }
  def main(args: Array[String]): Unit = {
    println(discount(150.0))
  }
}

No Need for Mutation

Because if and match are expressions, you can assign their result to a val instead of mutating a var.

This leads to cleaner, more functional code.

object Main {
  def main(args: Array[String]): Unit = {
    val score = 85
    val grade = if (score >= 90) "A"
      else if (score >= 80) "B"
      else "C"
    println(grade)
  }
}

Composing Expressions

Because everything returns a value, expressions nest and compose freely. The output of one becomes the input of another.

This composability is a big reason Scala feels expressive.

object Main {
  def main(args: Array[String]): Unit = {
    val n = 7
    val message = "n is " + (if (n % 2 == 0) "even" else "odd")
    println(message)
  }
}

Why It Matters

Expression-oriented code:

  • Reduces the need for mutable variables
  • Makes the flow of values explicit
  • Composes naturally into larger programs
  • Pairs perfectly with immutability

All Together

Here a block expression contains an if expression, all assigned to a single immutable result.

object Main {
  def main(args: Array[String]): Unit = {
    val total = {
      val items = 3
      val each = 12
      val raw = items * each
      if (raw > 30) raw - 5 else raw
    }
    println(total)
  }
}

Quick Check

Check your grasp of expressions.

Recap

You learned that in Scala nearly everything is an expression:

  • if/else, match, and blocks all return values
  • A block's value is its last expression
  • Side-effecting expressions return Unit (())
  • The last expression of a function is its return value
  • This reduces mutation and improves composability

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 is an expression. 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 3 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. Using the REPL
  2. val, var and Types
  3. Expressions over Statements
  4. String Interpolation
← Back to Scala for Backend Engineering & Functional Programming