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