Reputation: 17
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
Reputation: 37842
Make sure you follow these rules:
if
keyword should be followed by a conditionelse
clause can only follow an if
clauseApplied 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