Lesson 11: WHEN Condition with ARGUMENT in kotlin language

Lesson 11: WHEN Condition with ARGUMENT in kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

WHEN Condition with ARGUMENT in kotlin language. We have a second way to use the when statement in the Kotlin language is by passing an argument immediately after the keyword when.

Topic: WHEN Condition with ARGUMENT in kotlin language.

Problem 1

Enter an integer value between 1 and 5. Show the same in words .

Project66 – Home.kt

fun main (parameter: Array <String>) {
    print ("Enter an integer value between 1 and 5:")
    val value = readLine () !!. toInt ()
    when (value) {
        1 -> print ("one")
        2 -> print ("two")
        3 -> print ("three")
        4 -> print ("four")
        5 -> print ("five")
        else -> print ("value out of range")
    }
}
Lesson 11: WHEN Condition with ARGUMENT in kotlin language - Lesson 11: WHEN Condition with ARGUMENT in kotlin language - Lesson 11: WHEN Condition with ARGUMENT in kotlin language - WHEN Condition with ARGUMENT in kotlin language
WHEN Condition with ARGUMENT in kotlin language

As we see then we have the keyword when in brackets a variable. The contents of the variable “value” are verified with each of the indicated data.
For example if we load by keyboard 3 then the first two paths are false:

         1 -> print ("one")
         2 -> print ("two")

But the third path is verified true and goes to execute the commands arranged after the operator ->

         3 -> print ("three")

Topic: WHEN Condition with ARGUMENT in kotlin language.

Problem 2

Enter a positive integer value between 1 and 10000. Print a message indicating how many digits you have.

Project67 – Principal.kt

fun main (parameter: Array <String>) {
    print ("Enter a positive integer value between 1 and 99999:")
    val value = readLine () !!. toInt ()
    when (value){
    in 1..9 -> print ("Has 1 digit")
    in 10..99 -> print ("It has 2 digits")
    in 100..999 -> print ("It has 3 digits")
    in 1000..9999 -> print ("It has 4 digits")
    in 10000..99999 -> print ("It has 5 digits")
    else -> print ("Not in the indicated range")
}
}

Also in Kotlin we can check if a variable is within a certain range using the word in and the respective range.

Problem 3

Enter 10 integer values per keyboard. Count how many of these entered values were zero and how many 1.5 or 10.

Topic: WHEN Condition with ARGUMENT in kotlin language.

Project68 – Principal.kt

fun main (parameter: Array <String>) {
    var cant1 = 0
    var cant2 = 0
    for (i in 1..10) {
        print ("Enter an integer value:")
        val value = readLine () !!. toInt ()
        when (value){
        0 -> cant1 ++
        1, 5, 10 -> cant2 ++
    }
}
println ("Number of numbers 0 entered: $cant1")
println ("Number of numbers 1,2 or 3 entered: $cant2")
}

In this problem we have in one of the paths of the when a list of values separated by commas, if any of them matches the variable “value” then “cant2” is incremented:

             1, 5, 10 -> cant2 ++

Proposed problem

  • Charge the number of children of 10 families. Count how many have 0.1.2 or more children. Print these counters.

Solution

Topic: WHEN Condition with ARGUMENT in kotlin language.

About The Author

Related posts

Leave a Reply