What will be the result of the following code execution?

for (
  i1 <- 1 to 5
  if i1 % 2 == 0
  i2 <- 1 to i1
  if i2 > 2
) println(i2)
Explanation
The Scala compiler will not infer semicolons while inside parentheses. That's why it is advantageous to use curly braces, because you can leave off some of the semicolons that are needed when you use parentheses. The following code will be successfully compiled:

for (
  i1 <- 1 to 5
  if i1 % 2 == 0;
  i2 <- 1 to i1
  if i2 > 2
) println(i2)

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes