Browse Source

Merge pull request #4440 from Calinou/shading-language-ternary-operator

Document the ternary operator in Shading language
Yuri Roubinsky 4 years ago
parent
commit
48069fe078
1 changed files with 20 additions and 12 deletions
  1. 20 12
      tutorials/shaders/shader_reference/shading_language.rst

+ 20 - 12
tutorials/shaders/shader_reference/shading_language.rst

@@ -448,42 +448,50 @@ Godot Shading language supports the most common types of flow control:
 
 
 .. code-block:: glsl
 .. code-block:: glsl
 
 
-    // if and else
+    // `if` and `else`.
     if (cond) {
     if (cond) {
 
 
     } else {
     } else {
 
 
     }
     }
 
 
-    // switch
-    switch (i) { // signed integer expression
+    // Ternary operator.
+    // This is an expression that behaves like `if`/`else` and returns the value.
+    // If `cond` evaluates to `true`, `result` will be `9`.
+    // Otherwise, `result` will be `5`.
+    int result = cond ? 9 : 5;
+
+    // `switch`.
+    switch (i) { // `i` should be a signed integer expression.
         case -1:
         case -1:
             break;
             break;
         case 0:
         case 0:
-            return; // break or return
-        case 1: // pass-through
+            return; // `break` or `return` to avoid running the next `case`.
+        case 1: // Fallthrough (no `break` or `return`): will run the next `case`.
         case 2:
         case 2:
             break;
             break;
         //...
         //...
-        default: // optional
+        default: // Only run if no `case` above matches. Optional.
             break;
             break;
     }
     }
 
 
-    // for loops
+    // `for` loop. Best used when the number of elements to iterate on
+    // is known in advance.
     for (int i = 0; i < 10; i++) {
     for (int i = 0; i < 10; i++) {
 
 
     }
     }
 
 
-    // while
-    while (true) {
+    // `while` loop. Best used when the number of elements to iterate on
+    // is not known in advance.
+    while (cond) {
 
 
     }
     }
 
 
-    // do while
-    // Like `while`, but always runs at least once.
+    // `do while`. Like `while`, but always runs at least once even if `cond`
+    // never evaluates to `true`.
     do {
     do {
 
 
-    } while (true);
+    } while (cond);
 
 
 Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze
 Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze
 your application (including editor). Godot can't protect you from this, so be
 your application (including editor). Godot can't protect you from this, so be