HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to ScalaVariables (val / var)Data TypesStringsListsConditions (if / match)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Collection
🔀 Condition & Loop
🧩 Function
Scala›Conditions (if / match)

Conditions (if / match)

Decision making with if-else, if returning a value, and pattern matching with match.

Simple enough for a 5-year-old to understand

if / else

Example code
1
val number = 7
2
if number % 2 == 0 then
Is it evenly divisible by two?
ConditionIs it evenly divisible by two?
3
println("even")
If the condition is true.
Output / InputIf the condition is true.
4
else
5
println("odd")
If the condition is false.
Output / InputIf the condition is false.
ConditionOutput / Input
💡 Hint: Equality check is done with a double equals sign.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Which operator checks for the equality of two values?

About this topic

In Scala, if is an expression and produces a value. Therefore, a separate ternary operator is not needed; you can assign the result of a condition directly to a variable.

Pattern matching is used for multi-branch decisions. Scala's match structure is much more powerful than C's switch structure: it can match against constants, types, collection shapes, and conditional patterns.

There is a trap in the match structure: if no branch matches, a runtime error occurs. If you are working with a finite type, the compiler can warn about missing branches; otherwise, it is your responsibility to add a catch-all branch.

What you will learn

  • making the if expression produce a value
  • Pattern matching
  • The exhaustiveness rule
  • Guard conditions
  • Logical operators

Who is this for?

  • Learners of control flow
  • Those coming from Java and looking for a ternary operator
  • Those seeing pattern matching for the first time

When to use?

if is sufficient for two-option decisions; if you are branching based on the shape or type of a value, pattern matching is both concise and safe.

Pattern matching is the most natural way to unwrap Option and result types, forcing you to handle all cases.

Common mistakes

Wrong

val sayi = 5
val sonuc = sayi match {
  case 1 => "one"
  case 2 => "two"
}
println(sonuc)

A MatchError is thrown because no branch matches, and the program stops. The compiler cannot always warn about omissions in a type that can take infinite values. It is necessary to add a general catch-all branch.

Correct

val sayi = 5
val sonuc = sayi match {
  case 1 => "one"
  case 2 => "two"
  case _ => "other"
}
println(sonuc)

Wrong

val sayi = 5
if (sayi > 10)
  println("big")
  println("always runs")

Since there are no curly braces, only the first line is bound to the condition; the second line runs in all cases. Indentation has no meaning in Scala 2, and this code is misleading. Using a block, even for a single line, prevents this error.

Correct

val sayi = 5
if (sayi > 10) {
  println("big")
  println("this is also conditional")
}

Wrong

val deger: Any = 5
val sonuc = deger match {
  case _ => "other"
  case i: Int => "integer"
}

Branches are tried from top to bottom, and since the general branch is first, the second one is never reached; the compiler warns with "unreachable code". Specific cases must always be written before the general case.

Correct

val deger: Any = 5
val sonuc = deger match {
  case i: Int => "integer"
  case _ => "other"
}

Frequently asked questions

How do you add a guard in pattern matching?

You write a condition at the end of the branch; even if the pattern matches, if the condition is not met, that branch is skipped. It is used for numeric ranges and extra validations, saving you from writing nested conditions.

When does the compiler warn about missing branches?

When possible values are finite and types are known by the compiler: such as sealed class hierarchies and enumerations. Therefore, building domain models with sealed types ensures that all match points emit a warning when a new state is added.

Should I always use match instead of if?

No. For a simple truth check, if is more readable. Match should be preferred when branching based on the value's structure or type, or when handling multiple cases.

Summary

if is an expression, it produces a value.

Match branches are tried from top to bottom.

If no branch matches, a runtime error occurs.

The catch-all branch must be at the very end.

Guards add extra conditions to a branch.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code