|
1 | 1 | # Control Flows: Conditions and Iterations |
2 | 2 |
|
| 3 | +**Comparison** and **Logical** operators are used in *conditional* statements and serve to test for `ture` or `false`. |
| 4 | + |
| 5 | +## Comparison Operators |
| 6 | + |
| 7 | +The Comparison operators are used in logical statements to determine equality or difference between variables or values. |
| 8 | + |
| 9 | +Let's assume that `x = 5`. The table below explains the comparison operators: |
| 10 | + |
| 11 | +| Operator | Description | Comparing | Returns | |
| 12 | +|--------------------|-------------------------------------|-----------|----------| |
| 13 | +| == | equal to | x == 8 | false | |
| 14 | +| == | equal to | x == 5 | true | |
| 15 | +| == | equal to | x == "5" | true | |
| 16 | +| | | | | |
| 17 | +| === | equal value and equal type | x === 5 | true | |
| 18 | +| === | equal value and equal type | x === "5" | false | |
| 19 | +| | | | | |
| 20 | +| != | not equal | x != 8 | true | |
| 21 | +| | | | | |
| 22 | +| !== | not equal value or not equal type | x !== 5 | false | |
| 23 | +| !== | not equal value or not equal type | x !== "5" | true | |
| 24 | +| !== | not equal value or not equal type | x !== 8 | true | |
| 25 | +| | | | | |
| 26 | +| > | grater than | x > 8 | false | |
| 27 | +| < | less than | x < 8 | true | |
| 28 | +| >= | grater than or equal to | x >= 8 | false | |
| 29 | +| <= | less than or equal to | x <= 8 | true | |
| 30 | + |
| 31 | +## Logical Operators |
| 32 | + |
| 33 | +The logical operators are used to determine the logic between variables or values. Let's take that `x = 6` and `y = 3`. The table below explains the logical operators: |
| 34 | + |
| 35 | +| Operator | Description | Example | |
| 36 | +|--------------------|-------------------------------------|------------------------------| |
| 37 | +| && | and | (x < 10 && y > 1) is true | |
| 38 | +| == | or | (x == 5 || y == 5) is false | |
| 39 | +| == | not | !(x == y) is true | |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
3 | 45 | ## Conditions |
4 | 46 |
|
5 | 47 | The main conditional statement is <b>*if*</b>. Within this parameter, all the available <a href="../placeholders/" target="_blank">placeholders</a> and their objective JavaScript mappings can be used. |
@@ -204,7 +246,7 @@ onInstall: |
204 | 246 | ``` |
205 | 247 | @@! |
206 | 248 |
|
207 | | -### **Else** |
| 249 | +### Else |
208 | 250 |
|
209 | 251 | In case the conditional statement should be complemented by the opposite comparison and respective action the ***else*** conditional operator can be accommodated. |
210 | 252 |
|
@@ -431,6 +473,61 @@ onInstall: |
431 | 473 | ``` |
432 | 474 | @@! |
433 | 475 |
|
| 476 | +### Single line *if* statement |
| 477 | + |
| 478 | +Another ***if-else*** combination can be represented as a single ***if*** statement when multiple conditions are required to be checked and the statements nesting is not mandatory. |
| 479 | +It is applicable if any condition or all of the conditions in the statement may lead to the same outcome. |
| 480 | +For example: |
| 481 | + |
| 482 | +@@@ |
| 483 | +```yaml |
| 484 | +type: install |
| 485 | +name: '[CS:Conditions] - action single line "if"' |
| 486 | + |
| 487 | +globals: |
| 488 | + a: 1 |
| 489 | + b: 2 |
| 490 | + |
| 491 | +onInstall: |
| 492 | +- log: "-- single line if test --" |
| 493 | +- if ((globals.b == 2) && (globals.a == 1) && (globals.a == 1)): |
| 494 | + assert: true |
| 495 | + |
| 496 | +- log: "-- another single line if test --" |
| 497 | +- if (globals.a == 2 || globals.a == 3 || globals.a == 1): |
| 498 | + assert: true |
| 499 | +``` |
| 500 | +```json |
| 501 | +{ |
| 502 | + "type": "install", |
| 503 | + "name": "[CS:Conditions] - action single line \"if\"", |
| 504 | + "globals": { |
| 505 | + "a": 1, |
| 506 | + "b": 2 |
| 507 | + }, |
| 508 | + "onInstall": [ |
| 509 | + { |
| 510 | + "log": "-- single line if test --" |
| 511 | + }, |
| 512 | + { |
| 513 | + "if ((globals.b == 2) && (globals.a == 1) && (globals.a == 1))": { |
| 514 | + "assert": true |
| 515 | + } |
| 516 | + }, |
| 517 | + { |
| 518 | + "log": "-- another single line if test --" |
| 519 | + }, |
| 520 | + { |
| 521 | + "if (globals.a == 2 || globals.a == 3 || globals.a == 1)": { |
| 522 | + "assert": true |
| 523 | + } |
| 524 | + } |
| 525 | + ] |
| 526 | +} |
| 527 | +``` |
| 528 | +@@! |
| 529 | + |
| 530 | + |
434 | 531 | ## Iterations |
435 | 532 |
|
436 | 533 | ### ForEach |
|
0 commit comments