浏览代码

Ternary operator documentation.

Vladislav Folts 9 年之前
父节点
当前提交
e0e9b44ad4
共有 2 个文件被更改,包括 29 次插入0 次删除
  1. 28 0
      doc/wiki/eberon-ternary-operator.md
  2. 1 0
      doc/wiki/eberon.md

+ 28 - 0
doc/wiki/eberon-ternary-operator.md

@@ -0,0 +1,28 @@
+Ternary operator can be used as a shortcut for the IF/ELSE statement.
+
+### Syntax
+
+	condition ? a : b
+
+_condition_ is a boolean expression.  
+_a_ and _b_ are expressions to evaluate. The operator returns _a_ when condition is TRUE and _b_ when condition is FALSE.
+
+### Example
+
+	PROCEDURE max(a, b: INTEGER): INTEGER;
+	VAR
+		result: INTEGER;
+	BEGIN
+		IF a > b THEN
+			result := a;
+		ELSE
+			result := b;
+		END;
+		RETURN result;
+	END;
+
+This code above may be rewritten in much shorter and cleaner manner using ternary operator:
+
+	PROCEDURE max(a, b: INTEGER): INTEGER;
+		RETURN a > b ? a : b;
+	END;

+ 1 - 0
doc/wiki/eberon.md

@@ -15,6 +15,7 @@ Eberon basically extends original Oberon (excluding additional [restrictions](#r
 * [[Record fields read-only export|eberon-record-fields-read-only-export]]
 * [[Procedure call result can be denoted|eberon-procedure-call-result]]
 * [[Operator NEW|eberon-operator-NEW]]
+* [[Ternary operator||eberon-ternary-operator]]
 * [[Syntax relaxations|eberon-syntax-relaxations]]
 * Non-scalar variables (arrays and records) can be exported (forbidden in oberon for some unknown reason).