Kotlin When With Multiple Conditions

[Solved] Kotlin When With Multiple Conditions | Kotlin - Code Explorer | yomemimo.com
Question : kotlin filter multiple conditions

Answered by : abdullah-0kvpuik7ilzv

for (i in dnsTestViewModel.msList.indices) { Log.println(Log.ASSERT, "PS", "MS :"+ dnsTestViewModel.msList[i]) if(dnsTestViewModel.msList[i] != longm) { for (j in 0 until dnsTestViewModel.msList.size - 1 - i) { if (dnsTestViewModel.msList[j] > dnsTestViewModel.msList[j + 1] && dnsTestViewModel.msList[j] > 0 && dnsTestViewModel.msList[j + 1] > 0) { val tempMs = dnsTestViewModel.msList[j] val tempdns = dnsTestViewModel.tempdnsListFirst[j] dnsTestViewModel.msList[j] = dnsTestViewModel.msList[j + 1] dnsTestViewModel.tempdnsListFirst[j] = dnsTestViewModel.tempdnsListFirst[j + 1] dnsTestViewModel.msList[j + 1] = tempMs dnsTestViewModel.tempdnsListFirst[j + 1] = tempdns } } } }

Source : | Last Update : Tue, 24 Aug 21

Question : Kotlin when multiple conditions

Answered by : hannan

When expression
when defines a conditional expression with multiple branches.
It is similar to the switch statement in C-like languages.
Its simple form looks like this. when (x) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { print("x is neither 1 nor 2") } }
when matches its argument against all branches sequentially
until some branch condition is satisfied.
when can be used either as an expression or as a statement.
If it is used as an expression, the value of the first matching
branch becomes the value of the overall expression. If it is
used as a statement, the values of individual branches are ignored.
Just like with if, each branch can be a block, and its value is the
value of the last expression in the block.
The else branch is evaluated if none of the other branch conditions
are satisfied. If when is used as an expression, the else branch is
mandatory, unless the compiler can prove that all possible cases are
covered with branch conditions, for example, with enum class entries
and sealed class subtypes). enum class Bit { ZERO, ONE } val numericValue = when (getRandomBit()) { Bit.ZERO -> 0 Bit.ONE -> 1 // the 'else' clause is not required because all cases are covered }
To define a common behavior for multiple cases, combine their conditions
in a single line with a comma: when (x) { 0, 1 -> print("x == 0 or x == 1") else -> print("otherwise") }
You can use arbitrary expressions (not only constants) as branch conditions when (x) { s.toInt() -> print("s encodes x") else -> print("s does not encode x") }
You can also check a value for being in or !in a range or a collection: when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") }
Another option is checking that a value is or !is of a particular type.
Note that, due to smart casts, you can access the methods and properties
of the type without any extra checks. fun hasPrefix(x: Any) = when(x) { is String -> x.startsWith("prefix") else -> false }
when can also be used as a replacement for an if- else if chain.
If no argument is supplied, the branch conditions are simply boolean
expressions, and a branch is executed when its condition is true: when { x.isOdd() -> print("x is odd") y.isEven() -> print("y is even") else -> print("x+y is odd") }
You can capture when subject in a variable using following syntax: fun Request.getBody() = when (val response = executeRequest()) { is Success -> response.body is HttpError -> throw HttpException(response.status) }
The scope of variable introduced in when subject is restricted to the
body of this when.

Source : | Last Update : Sat, 20 Nov 21

Question : kotlin when with multiple conditions

Answered by : smoggy-snail-soz6mfud8cd7

fun main() { val x = 1 val square = when (x) { 1 -> 1 2 -> 4 3 -> 9 else -> 0 } println(square)
}

Source : https://tedblob.com/kotlin-when-example/ | Last Update : Sat, 30 Oct 21

Answers related to kotlin when with multiple conditions

Code Explorer Popular Question For Kotlin