Press enter to see results or esc to cancel.

Lesson 11: WHEN Condition with ARGUMENT in kotlin language

Share this
()

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.

[sociallocker id=”958″]

 Project69
fun main(parametro: Array<String>){
    var count1 = 0
    var count2 = 0
    for(i in 1..10) {
        print("Ingrese un valor entero:")
        val valor = readLine()!!.toInt()
        when (valor){
            0 -> count1++
            1, 5, 10 -> count2++
        }
    }
    println("Number of families with 0 children: $count1")
    println("Number of families 1,2  3 children: $count2")
}

[/sociallocker]

How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this