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

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.

主站蜘蛛池模板: 霍州市| 浑源县| 武胜县| 和田县| 武陟县| 沅江市| 桦甸市| 扶风县| 清苑县| 阿拉善右旗| 尼玛县| 新乐市| 姚安县| 南和县| 白山市| 杭锦后旗| 海兴县| 延长县| 安义县| 叶城县| 阜南县| 调兵山市| 琼中| 九台市| 盈江县| 榆中县| 萍乡市| 息烽县| 和田市| 江安县| 合江县| 阿拉善盟| 柘城县| 文山县| 北票市| 奉新县| 彰武县| 揭阳市| 枣庄市| 高阳县| 镇雄县|