Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 2: Manage Control Flow

17. Challenge: Use When Expressions

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 16. Simplify Code with When Expressions Next episode: 18. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

In this challenge, you are going to fortify your knowledge of when expressions. For this, you have to use a when expression to return in which century a year of your choosing is. You have to cover at least three past centuries, and for all the cases, return a pre-defined message. Then you print out the message. That's it. Now pause the video and try to solve the challenge. Then when you're done, hit the play button and check my solution (bouncy music) I'll start off by choosing a year. Then create a parameterized when statement. This allows you to match the year to values easily. Add the following cases to match the centuries. Because we're currently in 2022, you don't have to check the future. Now store the results of this when in a value like so. And finally, print it out. Run the project and you should see the message printed out. Yeah, you used range checks for the value and an else case to cover all the years or range spans you didn't cover. You also used the when as an expression to return a value and store it in the constant you printed out. Good job.