Ausdrücke statt Anweisungen
Alles ist ein Ausdruck
Ausdrücke statt Anweisungen ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Ausdrücke statt Anweisungen“ kostenlos?
Ja — der vollständige Text von „Ausdrücke statt Anweisungen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ausdrücke statt Anweisungen“?
Alles ist ein Ausdruck Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?
Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Ausdrücke statt Anweisungen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?
Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Die REPL verwenden
- val, var und Typen
- Ausdrücke statt Anweisungen
- String-Interpolation