4.2.4 When expression

with
if
The branches are the same,
when
Branches can also be used as expressions.

if
when
The branch is treated as an expression, and the value of the code block of the qualified branch is the value of the entire expression. with
if
Similarly, if the body of the branch is a code block, the value of that code block is the value of the last expression in the block.

when
when
When statements are used as expressions,
when
The expression also needs to have a return value, so
when
Expressions are usually required
else
Branch, unless the compiler can detect that all possible cases have been covered.

The following code demonstrates this
when
Expression usage.

Program List:
Codes \ 04\4.2 \ WhenExpr kt

fun main(args: Array<String>){

var score = ‘B’

val str = when (score) {

‘A’ -> {

Println (” Hope you can make more progress “)

“Good”

}

‘B’ -> {

Println (” You don’t know what you’re capable of until you put it together “)

“Good”

}

‘C’ -> ‘middle’

‘D’ -> “Pass”

else -> {

Println (” Say nothing, come again “)

“Fail”

}

}

println(str)

}

The above procedure demonstrates
when
The use of expressions in
when
Each branch of an expression is guaranteed to have an expression defined at the end of the block as the value of the block
when
Expressions can return valid values. Run the above program to see the following output.

You don’t know what you’re capable of until you try

good

The first line of the output above is execution
when
The output of a particular branch of an expression. The second line of output is
when
The return value of an expression.

4.2.5 When branch handles scope

Through the use of
in
,
! in
Operator, we can also use
when
The branch checks whether an expression is in a specified range or collection. For example, the following code.

Program List:
Codes \ 04\4.2 \ WhenRange kt

fun main(args: Array<String>){

val age = java.util.Random().nextInt(100)

println(age)

// Assign a value to STR using the when expression

var str = when (age){

in 10.. 25 -> “I was young at that time”

in 26.. 50 -> “Scenery vaguely reminiscent of last year”

in 51.. 80 -> “Drunk to hear the sound of the orchestra”

Else ->”

}

println(str)

}

The above program uses
when
The expression of
age
Variables to determine the
when
Expressions are no longer required
age
Is equal to some specific value, as long as
age
Within a specific scope, you can enter the corresponding branch. Run the program and you see the following output.

39

The scenery looks vaguely like last year

From the above running results can be seen: when
age
Variable is
39
When, it is in
26.. 50
Between,
when
The expression will return the value of the branch.

4.2.6 When branch processing type

Through the use of
is
,
! is
Operator, we can also use
when
The branch checks whether the expression is of the specified type. For example, the following code.

Program List:
Codes \ 04\4.2 \ WhenTypeDetect kt

fun main(args: Array<String>){

var inputPrice = 26

println(realPrice(inputPrice))

}

// The program determines the type of inputPrice

fun realPrice(inputPrice: Any) = when(inputPrice){

// If inputPrice is of type String, the program returns a Double converted from that String

is String -> inputPrice.toDouble()

// If inputPrice is of type Int, the program returns a Double converted from that Int

is Int -> inputPrice.toDouble()

is Double -> inputPrice

The else – > 0.0

}

The above program uses
when
The expression of
inputPrice
The type of program to determine
inputPrice
Arguments can be of any type, so the program determines if
inputPrice
is
String
Type, is called
String
The object’s
toDouble()
Convert it to
Double
After return; if
inputPrice
is
Int
Type, is called
Int
the
toDouble
Convert it to
Double
After return; if
inputPrice
is
Double
Type, returns directly
inputPrice
.

4.2.7 When conditional branch

The when branch can also be used to replace if… Else if chain, in which case there is no need to provide any conditional expression for the WHEN branch, in which case each branch condition is a Boolean expression, and the branch is executed when the specified branch’s Boolean expression is true. For example, the following code.

Program List:
Codes \ 04\4.2 \ WhenFork kt

fun main(args: Array<String>){

Val ln = readLine()// ln is String? Type, so you need to check whether ln is null

if(ln ! = null){

// The when branch does not require any conditional expressions

when {

// Each branch condition is a Boolean expression

Matches (Regex(“\\d+”)) -> println(” You entered all numbers “)

Ln. Matches (Regex ([a zA – Z] + “”)) – > println (” you input all the letters”)

Ln. Matches (Regex (” [a zA – Z0-9] +)) – > println (” the letters and Numbers you entered “)

Else -> println(” Your input contains special characters “)

}

}

}

As you can see from the code above, at this point
when
Branches do not require any conditional expression, in which case each branch is a Boolean expression, when the Boolean expression of a branch is
true
When,
when
The branch code will be executed.

Run the above program, the program will feedback different output according to user input. As shown below.

124327

Your input is full of numbers

The above is excerpted from Crazy Kotlin Handouts: a handout that gives you the most direct insight into Kotlin’s madness



The book will be released in November 2017





The instalments

Issue 1: Juejin. Im /post/59c0b7…

Juejin. Im /post/59c1d6…

Issue 3: Juejin. Im/Post /59e407…

Issue 4: Juejin. Im/Post /59ed77…

Juejin. Im/Post / 59eEC3…

Juejin. Im/Post /59effb…

Juejin. Im/Post / 59F153…

Issue 8: Juejin. Im/Post / 59F283…

Issue 9: Juejin. Im/Post / 59F686…

Juejin. Im/Post / 59F7EA…

Issue 11: Juejin. Im/Post / 59F953…

Juejin. Im/Post /59fa7e…

Im/Post /59fc1e…

Issue 14: Juejin. Im/Post / 59ffCB…

Issue 15: Juejin. Im/POST / 5A0162…

Issue 16: Juejin. Im/POST / 5A0271…

Im/POST / 5A090f…

Issue 18:
Juejin. Im/post / 5 a0e40…

Issue 19: Juejin. Im/Post /5a13bc…

Im/Post / 5a14E8…

Issue 21: juejin.cn/post/684490…

Related book “Crazy Android handouts” item.jd.com/11689014.ht…