|  | 
| 1 | 1 | # When | 
| 2 | 2 | 
 | 
| 3 |  | -Instead of widely used `switch` statement, Kotlin provides the more flexible and clear `when` construction. It can be used either as a statement or as an expression. | 
|  | 3 | +Instead of the widely used `switch` statement, Kotlin provides the more flexible and clear `when` construction. It can be used either as a statement or as an expression. | 
| 4 | 4 | 
 | 
| 5 | 5 | ## When Statement | 
| 6 | 6 | 
 | 
| @@ -31,10 +31,10 @@ class MyClass | 
| 31 | 31 | </div> | 
| 32 | 32 | 
 | 
| 33 | 33 | 1. This is a `when` statement.  | 
| 34 |  | -2. Checking whether `obj` equals to one. | 
| 35 |  | -3. Checking whether `obj` equals to `Hello`. | 
| 36 |  | -4. Type checking. | 
| 37 |  | -5. Inverse type checking. | 
|  | 34 | +2. Checks whether `obj` equals to one. | 
|  | 35 | +3. Checks whether `obj` equals to `Hello`. | 
|  | 36 | +4. Performs type checking. | 
|  | 37 | +5. Performs inverse type checking. | 
| 38 | 38 | 6. Default statement (might be omitted). | 
| 39 | 39 | 
 | 
| 40 | 40 | Note that all branch conditions are checked sequentially until one of them is satisfied. So, only the first suitable branch will be executed.    | 
| @@ -65,7 +65,7 @@ fun whenAssign(obj: Any): Any { | 
| 65 | 65 | </div> | 
| 66 | 66 | 
 | 
| 67 | 67 | 1. This is a `when` expression.  | 
| 68 |  | -2. Setting the value to `"one"` if `obj` equals to one. | 
| 69 |  | -3. Setting the value to one if `obj` equals to `Hello`. | 
| 70 |  | -4. Setting the value to `false` if `obj` is an instance of `Long`. | 
| 71 |  | -5. Setting the value "42" if none of the previous conditions are satisfied. Unlike in `when` _statement_, the default branch is usually required in `when` _expression_, except the case when the compiler can check that other branches cover all possible cases. | 
|  | 68 | +2. Sets the value to `"one"` if `obj` equals to one. | 
|  | 69 | +3. Sets the value to one if `obj` equals to `Hello`. | 
|  | 70 | +4. Sets the value to `false` if `obj` is an instance of `Long`. | 
|  | 71 | +5. Sets the value "42" if none of the previous conditions are satisfied. Unlike in `when` _statement_, the default branch is usually required in `when` _expression_, except the case when the compiler can check that other branches cover all possible cases. | 
0 commit comments