Search⌘ K
AI Features

Exercise: Repeating Conditional Operations

Explore how to implement conditional operations using Kotlin's when statement and type checks. Understand how to safely cast variables and use loops to perform repeated conditional actions. This lesson provides hands-on practice with type-safe coding and control flow in Kotlin.

We'll cover the following...

Problem statement

You are given two variables:

val num: Any = 42
val str: Any = "Hello, Kotlin!"

Perform the following conditional operations based on the types of the given variables:

  1. If num is of type Int and greater than 50, multiply it by 2 and print the result.

  2. If num is of type Int or Long, concatenate it ...