mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-05 22:09:56 +03:00
Merge pull request #4440 from Calinou/shading-language-ternary-operator
Document the ternary operator in Shading language
This commit is contained in:
@@ -448,42 +448,50 @@ Godot Shading language supports the most common types of flow control:
|
||||
|
||||
.. code-block:: glsl
|
||||
|
||||
// if and else
|
||||
// `if` and `else`.
|
||||
if (cond) {
|
||||
|
||||
} 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:
|
||||
break;
|
||||
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:
|
||||
break;
|
||||
//...
|
||||
default: // optional
|
||||
default: // Only run if no `case` above matches. Optional.
|
||||
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++) {
|
||||
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
} while (true);
|
||||
} while (cond);
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user