user7724005
user7724005

Reputation: 17

Nested If, Else If, Else

I've just got onto nested if statements in Scala and I've seen examples on here which suggest this syntax is correctly however the { under else if and second to last } are giving me "( expected" and invalid start of expression.

object NestedIf2 {
  def main(args: Array[String]): Unit = {
    var x = 30
    var y = 10

    if ( x == 30 ) {
      if ( y == 10 ) {
        println("X = 30 and Y = 10")
      }
    } else if {
      if ( y == 20 ) {
        println("X = 30 and Y = 20")
      }
    } else {
      if ( y == 30 ) {
        println("X = 30 and Y = 30")
      }
    }       
  }
}

Upvotes: 1

Views: 3786

Answers (1)

Tzach Zohar
Tzach Zohar

Reputation: 37842

Make sure you follow these rules:

  • Every if keyword should be followed by a condition
  • An else clause can only follow an if clause

Applied to your code:

if (x == 30) {
  if (y == 10) {
    println("X = 30 and Y = 10")
  } else if (y == 20) {
    println("X = 30 and Y = 20")
  } else if (y == 30) {
    println("X = 30 and Y = 30")
  }
}

Upvotes: 3

Related Questions