Browse Source

Merge pull request #5071 from Calinou/gdscript-nested-ternary-if

Document nested ternary-if usage example in GDScript reference
Max Hilbrunner 4 years ago
parent
commit
5921c629b4
1 changed files with 23 additions and 0 deletions
  1. 23 0
      tutorials/scripting/gdscript/gdscript_basics.rst

+ 23 - 0
tutorials/scripting/gdscript/gdscript_basics.rst

@@ -1006,6 +1006,29 @@ boolean expression. In this case, ternary-if expressions come in handy::
     var x = [value] if [expression] else [value]
     y += 3 if y < 10 else -1
 
+Ternary-if expressions can be nested to handle more than 2 cases. When nesting
+ternary-if expressions, it is recommended to wrap the complete expression over
+multiple lines to preserve readability::
+
+    var count = 0
+
+    var fruit = (
+            "apple" if count == 2
+            else "pear" if count == 1
+            else "banana" if count == 0
+            else "orange"
+    )
+    print(fruit)  # banana
+
+    # Alternative syntax with backslashes instead of parentheses (for multi-line expressions).
+    # Less lines required, but harder to refactor.
+    var fruit_alt = \
+            "apple" if count == 2 \
+            else "pear" if count == 1 \
+            else "banana" if count == 0 \
+            else "orange"
+    print(fruit_alt)  # banana
+
 while
 ^^^^^