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

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.

主站蜘蛛池模板: 册亨县| 上蔡县| 通化县| 永嘉县| 华安县| 长武县| 宜章县| 舞阳县| 萍乡市| 黎川县| 象山县| 中方县| 泰兴市| 会泽县| 韶山市| 鹿泉市| 邵阳市| 乌恰县| 上饶市| 涟源市| 昔阳县| 平昌县| 西峡县| 奇台县| 同江市| 三穗县| 琼海市| 吴旗县| 佛教| 汤原县| 浦江县| 瑞昌市| 丽水市| 朝阳县| 抚州市| 贵阳市| 贡嘎县| 米脂县| 新闻| 张北县| 金寨县|