Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q6 of 15
Kotlin - Loops and Ranges
Identify the error in this Kotlin code snippet:
for (i in 1..5) {
    if (i == 3) continue else break
    println(i)
}
ARuntime error: infinite loop.
BLogical error: loop will never break.
CNo error, code runs fine.
DSyntax error: 'else' cannot be used with 'continue' like this.
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of 'if' with 'continue' and 'else'

    In Kotlin, if (i == 3) continue else break is not valid syntax because 'continue' and 'else' must be separated properly with braces.
  2. Step 2: Identify the error

    The code will cause a syntax error due to improper use of 'else' with 'continue' without braces.
  3. Final Answer:

    Syntax error: 'else' cannot be used with 'continue' like this. -> Option D
  4. Quick Check:

    Improper if-else syntax with continue/break causes error [OK]
Quick Trick: Use braces for if-else with continue/break [OK]
Common Mistakes:
MISTAKES
  • Assuming syntax error without braces
  • Thinking continue and else cannot combine
  • Overlooking valid control flow syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes