Lesson 4 of 15

If Expressions

If Expressions

In Scala, if/else is an expression — it returns a value:

val score = 85
val grade = if (score >= 90) "A" else if (score >= 80) "B" else "C"
println(grade)  // B

This means you can use if/else directly as a function body:

def max(a: Int, b: Int): Int = if (a > b) a else b

Comparison Operators

==, !=, <, >, <=, >=

Logical Operators

&& (and), || (or), ! (not)

Your Task

Write a function sign that takes an Int and returns:

  • "positive" if greater than 0
  • "negative" if less than 0
  • "zero" if equal to 0
JS Transpiler loading...
Loading...
Click "Run" to execute your code.