官术网_书友最值得收藏!

Expressions over statements

A statement is an element of a program that represents an action. An expression is a part of the statement that gets evaluated to a value. Statements introduce mutability. The more statements a program contains, the more mutability it will have. Mutability in code increases the chance that it is erroneous. Expressions, on the other hand, do not produce mutability. In purely functional constructs, there are no statements, there are only expressions. More expressions in a program mean less mutability and code that is more concise.

Consider the code for 5_Expressions.kts:

val number = 5
val evenOrOdd = if(number % 2 == 0) "is even number" else "is odd number"
println("$number $evenOrOdd")

The output is as follows:

In this case, the code has expressions and we are not mutating any state. We are simply assigning the result of an expression to a variable and printing it.

Similarly, try...catch is also an expression. try...catch is used for handling the exceptions. The last statement in the try block becomes the expression for the try block.

主站蜘蛛池模板: 泸州市| 志丹县| 高尔夫| 闻喜县| 永昌县| 齐齐哈尔市| 秦皇岛市| 钦州市| 定兴县| 扶风县| 怀宁县| 邵阳市| 榕江县| 祁连县| 土默特右旗| 沁源县| 五莲县| 清新县| 庆阳市| 呼和浩特市| 张掖市| 阿巴嘎旗| 平邑县| 临沂市| 上饶县| 原阳县| 南平市| 化州市| 会同县| 烟台市| 进贤县| 林甸县| 西青区| 甘洛县| 南乐县| 綦江县| 张家界市| 清镇市| 如东县| 阿克苏市| 绵阳市|