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

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.

主站蜘蛛池模板: 新密市| 宜昌市| 岱山县| 龙川县| 米泉市| 南皮县| 县级市| 平原县| 灵山县| 平利县| 贡嘎县| 远安县| 永宁县| 英山县| 大化| 鹤壁市| 永寿县| 雷州市| 灵武市| 泗水县| 金山区| 许昌县| 佛山市| 紫阳县| 宁明县| 安顺市| 灌南县| 长春市| 永年县| 神农架林区| 昌黎县| 广东省| 年辖:市辖区| 山东省| 峨眉山市| 莱阳市| 靖边县| 绥中县| 正宁县| 泽普县| 恩平市|