Bird
0
0

Why does Kotlin allow multiple conditions separated by commas in a single when branch instead of using logical operators like && or ||?

hard📝 Conceptual Q10 of 15
Kotlin - Control Flow as Expressions
Why does Kotlin allow multiple conditions separated by commas in a single when branch instead of using logical operators like && or ||?
ABecause Kotlin does not support logical operators at all.
BBecause commas represent OR logic, making code clearer and concise.
CBecause && and || cause runtime errors in when expressions.
DBecause commas perform AND logic between conditions.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the meaning of commas in when branches

    Commas separate multiple values meaning OR logic, so the branch matches if any condition is true.
  2. Step 2: Compare with logical operators usage

    Logical operators like && and || are not allowed because they complicate syntax and reduce readability.
  3. Final Answer:

    Because commas represent OR logic, making code clearer and concise. -> Option B
  4. Quick Check:

    Commas mean OR logic for clarity in when = C [OK]
Quick Trick: Commas mean OR logic, clearer than && or || in when [OK]
Common Mistakes:
MISTAKES
  • Thinking && or || are allowed in when
  • Believing commas mean AND logic
  • Assuming logical operators cause runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes