Kotlin TutorialLesson 13Free preview
Kotlin When
This lesson is part of Kotlin · All levels
Summary
This lesson reviews the 'when' construction in Kotlin that replaced the traditional 'switch' statement with a much more flexible feature. 'when' matches the argument against the entire branch in sequence and can test single values, multiple values, number ranges, or data types (smart cast).
Key points
- The construction of 'when' is the modern substitute for 'switch' in other languages
- Branches connected with a thin arrow mark '->'
- Can test the range of numbers using the operator 'in range'
- Supports automatic smart casting if the branch checks the data type with 'ice Type'
- Can be used as both a normal statement and a return expression
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.