If » History » Version 1
Paul Janson, 11/26/2019 06:45 PM
1 | 1 | Paul Janson | h1. If |
---|---|---|---|
2 | |||
3 | */if (condition) <command>* |
||
4 | */if ((condition) combiner (condition)) <command>* |
||
5 | */if (condition operator condition) <command>* |
||
6 | */if (condition) { <command> | command }* |
||
7 | |||
8 | Conditional execution of code in a script or alias. Can also be used with /else and/or /elseif |
||
9 | Fills $v1 with either the condition-term value or the first of 2 conditions-terms, and fills $v2 with the value of the 2nd condition or with $null if there was only 1 condition. ($ifmatch is equivalent to $v1). When there are multiple groups of evaluated terms |
||
10 | While syntax is forgiving of non-use of parenthesis, it's probably best to use optional parenthesis around terms to improve readability, and in some cases ensures the correct values are filled into $v1 and $v2. |
||
11 | |||
12 | *Examples* |
||
13 | |||
14 | if ( $asctime($ctime,ddd) == Fri) echo -a today is Friday! |
||
15 | |||
16 | //if ( (1 > 2) || (4 > 3)) echo -a match $v1 > $v2 |
||
17 | fills $v1 with 4 and $v2 with 3 because those were the term1 and term2 in the 'true' condition causing the command to execute. If the 1 were changed to another number greater than 2, then $v2 would be filled with 2 and $v1 filled with the replacement for 1. |
||
18 | |||
19 | *|| = logical OR (if either condition is true) && = logical AND (only if both conditions are true)* |
||
20 | |||
21 | //if ($rand(1,2) == 2) echo -a message1 displays only if random is 2 | echo -a message2 always displays |
||
22 | //var %a 1 | if (%a) { echo -a message 1 says % $+ a does not contain number equivalent to 0 or $false or $ $+ null | echo -a message 2 } |
||
23 | By enclosing within braces, can have 2+ commands executed only in the 'true' condition. |
||
24 | You can negate the logic using the ! symbol: //if (!$query(0)) echo -a there are no query windows open |
||
25 | |||
26 | Using /else or /elseif can be on the same line separated by the pipe symbol or on a new line: |
||
27 | //var %a $rand(1,10) | if (%a > 5) echo -a roll is 6-10 | else echo -a roll is 1-5 |
||
28 | |||
29 | //var %a $rand(1,10) | if (%a > 5) echo -a roll is 6-10 |
||
30 | elseif (%a isnum 4-5) echo -a roll is 4 or 5 |
||
31 | else echo -a roll is 1-3 |