⚙️

Scala Conditions and Operators

Oct 11, 2025

Overview

This lecture explains how to formulate and use conditions in Scala with comparison operators to evaluate contexts and make decisions in programs.

Writing Conditions in Scala

  • Conditions are Boolean expressions that answer a specific question using available data.
  • To compare a variable (e.g., age) to a threshold, use operators like > or <.
  • Example: age > 20 evaluates if the user is older than 20, returning true or false.

Comparison Operators in Scala

  • Equality is checked with ==, not =, to avoid confusion with assignment.
  • Common comparison operators:
    • == tests equality
    • != tests inequality ("not equal")
    • < tests less than
    • > tests greater than
    • <= tests less than or equal to
    • >= tests greater than or equal to
  • Operators produce Boolean results: true or false.

Modeling Situations with Conditions

  • Define relevant variables and thresholds to represent real-world situations as conditions.
  • Example 1: Room temperature comfort
    • Variable: temperature
    • Condition: temperature > 14 (comfort if above 14°C)
  • Example 2: Car speed monitoring
    • Variable: speed
    • Condition: speed > 50 (car is too fast if speed is above 50 kph)
  • Example 3: Store order fulfillment
    • Variables: quantitécomm (quantity ordered), quantitéstock (quantity in stock)
    • Condition: quantitécomm > quantitéstock (cannot fulfill if order exceeds stock)

Key Terms & Definitions

  • Boolean expression — An expression that evaluates to true or false.
  • Comparison operator — A symbol (e.g., >, ==) used to compare two values.
  • Threshold — The reference value against which a variable is compared.
  • Assignment operator (=) — Sets a variable’s value.
  • Equality operator (==) — Tests if two values are equal.

Action Items / Next Steps

  • Practice writing conditions with comparison operators using variables and thresholds.
  • Prepare to represent everyday situations as Boolean expressions in Scala.