Conclusion
In this lesson you learned about the following topics:
- the
whenkeyword, - differences between the
whenandifstatements, - the statements and expressions,
- differences between exhaustive and non-exhaustive
whenstatements,
The when statement is a more flexible way of branching than if. It is especially useful when you have
multiple conditions to check. The syntax of a when is more compact and easier to read than
if. It can be used as a statement or an expression as well. It means that it can return a
value unlike a statement which does not return anything and has only side effects. Note that the when
expression has to be exhaustive. It means that it has to return a value from every branch. In most
of the cases, it means, there is an else branch required.
That is all for this lesson. In the next one, you will learn about the loops and how to repeat your code. See you there!