Skip to content

Commit

Permalink
Merge pull request #171 from JetBrains/when-else
Browse files Browse the repository at this point in the history
minor updates to documentation on 'when'
  • Loading branch information
abreslav committed Mar 24, 2015
2 parents 518e49a + 25f1c7f commit c0e65d0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions docs/reference/control-flow.md
Expand Up @@ -61,11 +61,16 @@ when (x) {
```

*when*{: .keyword } matches its argument against all branches consequently until some branch condition is satisfied.
*when*{: .keyword } is an expression and results in the satisfied branch's right hand side.
If some of its branches return result in a value of type `Unit`, the whole expression has type `Unit`.
Note that the *else*{: .keyword } branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions.
*when*{: .keyword } can be used either as an expression or as a statement. If it is used as an expression, the value
of the satisfied 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*{: .keyword }, each branch can be a block, and its value
is the value of the last expression in the block.)

If many cases should be handled in the same way, the branch conditions may be combined with a comma
The *else*{: .keyword } branch is evaluated if none of the other branch conditions are satisfied.
If *when*{: .keyword } is used as an expression, the *else*{: .keyword } branch is mandatory,
unless the compiler can prove that all possible cases are covered with branch conditions.

If many cases should be handled in the same way, the branch conditions may be combined with a comma:

``` kotlin
when (x) {
Expand Down Expand Up @@ -104,7 +109,7 @@ val hasPrefix = when(x) {
}
```

*when*{: .keyword } can also be used as a replacement for an *if*{: .keyword }-*else*{: .keyword }-*if*{: .keyword } chain.
*when*{: .keyword } can also be used as a replacement for an *if*{: .keyword }-*else*{: .keyword } *if*{: .keyword } chain.
If no argument is supplied, the branch conditions are simply boolean expressions, and a branch is executed when its condition is true:

``` kotlin
Expand All @@ -115,6 +120,8 @@ when {
}
```

See the [grammar for *when*{: .keyword }](grammar.html#when).


## For Loops

Expand Down

0 comments on commit c0e65d0

Please sign in to comment.