github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/switch.md (about) 1 # `switch` 2 3 > Blocks of cascading conditionals 4 5 ## Description 6 7 `switch` is a large block for simplifying cascades of conditional statements. 8 9 ## Usage 10 11 ``` 12 switch [value] { 13 case | if { conditional } [then] { code-block } 14 case | if { conditional } [then] { code-block } 15 ... 16 [ default { code-block } ] 17 } -> <stdout> 18 ``` 19 20 The first parameter should be either **case** or **if** -- the statements are 21 subtly different and thus alter the behavior of `switch`. 22 23 **then** is optional ('then' is assumed even if not explicitly present). 24 25 ## Examples 26 27 Output an array of editors installed: 28 29 ``` 30 switch { 31 if { which vi } { out vi } 32 if { which vim } { out vim } 33 if { which nano } { out nano } 34 if { which emacs } { out emacs } 35 } -> format: json 36 ``` 37 38 A higher/lower game written using `switch`: 39 40 ``` 41 function higherlower { 42 try { 43 rand int 100 -> set rand 44 while { $rand } { 45 read guess "Guess a number between 1 and 100: " 46 47 switch { 48 case: { = $guess < $rand } then { 49 out "Too low" 50 } 51 52 case: { = $guess > $rand } then { 53 out "Too high" 54 } 55 56 default: { 57 out "Correct" 58 let rand=0 59 } 60 } 61 } 62 } 63 } 64 ``` 65 66 String matching with `switch`: 67 68 ``` 69 read name "What is your name? " 70 switch $name { 71 case "Tom" { out "I have a brother called Tom" } 72 case "Dick" { out "I have an uncle called Dick" } 73 case "Sally" { out "I have a sister called Sally" } 74 default { err "That is an odd name" } 75 } 76 ``` 77 78 ## Detail 79 80 ### Comparing Values vs Boolean State 81 82 #### By Values 83 84 If you supply a value with `switch`... 85 86 ``` 87 switch value { ... } 88 ``` 89 90 ...then all the conditionals are compared against that value. For example: 91 92 ``` 93 switch foo { 94 case bar { 95 # not executed because foo != bar 96 } 97 case foo { 98 # executed because foo != foo 99 } 100 } 101 ``` 102 103 You can use code blocks to return strings too 104 105 ``` 106 switch foo { 107 case {out bar} then { 108 # not executed because foo != bar 109 } 110 case {out foo} then { 111 # executed because foo != foo 112 } 113 } 114 ``` 115 116 #### By Boolean State 117 118 This style of syntax could be argued as a prettier counterpart to if/else if. 119 Only code blocks are support and each block is checked for its boolean state 120 rather than string matching. 121 122 This is simply written as: 123 124 ``` 125 switch { ... } 126 ``` 127 128 ### When To Use `case`, `if` and `default`? 129 130 A `switch` command may contain multiple **case** and **if** blocks. These 131 statements subtly alter the behavior of `switch`. You can mix and match **if** 132 and **case** statements within the same `switch` block. 133 134 #### case 135 136 A **case** statement will only move on to the next statement if the result of 137 the **case** statement is **false**. If a **case** statement is **true** then 138 `switch` will exit with an exit number of `0`. 139 140 ``` 141 switch { 142 case { false } then { 143 # ignored because case == false 144 } 145 case { true } then { 146 # executed because case == true 147 } 148 case { true } then { 149 # ignored because a previous case was true 150 } 151 } 152 ``` 153 154 ### if 155 156 An **if** statement will proceed to the next statement _even_ if the result of 157 the **if** statement is **true**. 158 159 ``` 160 switch { 161 if { false } then { 162 # ignored because if == false 163 } 164 if { true } then { 165 # executed because if == true 166 } 167 if { true } then { 168 # executed because if == true 169 } 170 } 171 ``` 172 173 ### default 174 175 **default** statements are only run if _all_ **case** _and_ **if** statements are 176 false. 177 178 ``` 179 switch { 180 if { false } then { 181 # ignored because if == false 182 } 183 if { true } then { 184 # executed because if == true 185 } 186 if { true } then { 187 # executed because if == true 188 } 189 if { false } then { 190 # ignored because if == false 191 } 192 default { 193 # ignored because one or more previous if's were true 194 } 195 } 196 ``` 197 198 > **default** was added in Murex version 3.1 199 200 ### catch 201 202 **catch** has been deprecated in version 3.1 and replaced with **default**. 203 204 ## See Also 205 206 * [`!` (not)](../parser/not-func.md): 207 Reads the STDIN and exit number from previous process and not's it's condition 208 * [`and`](../commands/and.md): 209 Returns `true` or `false` depending on whether multiple conditions are met 210 * [`break`](../commands/break.md): 211 Terminate execution of a block within your processes scope 212 * [`catch`](../commands/catch.md): 213 Handles the exception code raised by `try` or `trypipe` 214 * [`false`](../commands/false.md): 215 Returns a `false` value 216 * [`if`](../commands/if.md): 217 Conditional statement to execute different blocks of code depending on the result of the condition 218 * [`let`](../commands/let.md): 219 Evaluate a mathematical function and assign to variable (deprecated) 220 * [`or`](../commands/or.md): 221 Returns `true` or `false` depending on whether one code-block out of multiple ones supplied is successful or unsuccessful. 222 * [`set`](../commands/set.md): 223 Define a local variable and set it's value 224 * [`true`](../commands/true.md): 225 Returns a `true` value 226 * [`try`](../commands/try.md): 227 Handles non-zero exits inside a block of code 228 * [`trypipe`](../commands/trypipe.md): 229 Checks for non-zero exits of each function in a pipeline 230 * [`while`](../commands/while.md): 231 Loop until condition false 232 233 <hr/> 234 235 This document was generated from [builtins/core/structs/switch_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/structs/switch_doc.yaml).