Control structure
<= =>
In centigrade the sentences in the definition are just executed sequentially, one after the other. To conditionally control which sentences are executed you use control structures.
Control structures are built with control words and sentences. The following is an example of a control structure:
if. x = 'c' do. centigrade y else. fahrenheit y end.
The if. control word starts the control structure and the end. control word ends it. The result of the x = 'c' sentence is the test result and it determines which of the other sentences in the control structure are executed. If the test result is 1, then the sentence after the do. control word is executed. If the test result is any other value then the sentence after the else. control word is executed. In English: if the left argument equals the letter c, then execute centigrade, otherwise execute fahrenheit.
Use this capability to add a new verb to your cf.ijs script that will convert a number from Fahrenheit to centigrade or from centigrade to Fahrenheit depending on the value of the left argument. Open your cf.ijs script and add the following definition at the end.
NB. convert f to c if x is 'c', otherwise c to f convert =: dyad : 0 if. x = 'c' do. centigrade y else. fahrenheit y end. )
This defines the dyadic case of the verb. The dyad has a left argument with the name x and a right argument with the name y . The verb convert takes a left argument of 'c' to convert a Fahrenheit value to centigrade. Any left argument other than 'c' will convert a centigrade value to Fahrenheit.
Note that you use your verbs fahrenheit and centigrade just as you would use primitive verbs.
Run the script and test convert.
'c' convert 212 100 'f' convert 100 212
Normally a sentence is a line in a script. However, with control words separating a line into several sentences it is possible to have more than one sentence on a line.
The following line is equivalent to the multiple lines used earlier.
if. x = 'c' do. centigrade y else. fahrenheit y end.
Control structures are only allowed in definitions and you cannot type one directly into the ijx window for execution.
There are nine control structure patterns:
if. T do. B end. if. T do. B else. B1 end. if. T do. B elseif. T1 do. B1 elseif. T2 do. B2 end. try. B catch. B1 end. while. T do. B end. whilst. T do. B end. for. T do. B end. for_i. T do. B end. select. T case. T0 do. B0 case. T1 do. B1 fcase.T2 do. B2 case. T3 do. B3 end.
A control structure starts with if., try. , while. , whilst. , for. , for_i. , or select. and ends with a matching end..
Words beginning with T or B denote a block of 0 or more sentences and can contain nested control structures.
The result of the last sentence in a T block determines which block is executed next and whether execution in the control structure is finished.
Often the T block is a single sentence that makes a simple test like the one in the example.
The try. control structure is an interesting one. It executes the B block of sentences, and if there are no errors it skips to the end of the structure. However, if there is an error, then the B1 block is executed.
The while. control structure executes the T block and if its result is not 0 then it executes the B block and continues this until the T block has a 0 result. If the T block is 0 the first time, then the B block is not executed.
The whilst. control structure is the same as while. except that the T block is skipped the first time. This means that the B block is always executed at least once.
In the for. structure, the B block is evaluated once for each item of the array A that results from evaluating the T block. In the for_i. form, the local name i is set to the value of an item on each evaluation of the B block and i_index is set to the index of the item. A break. goes to the end of the for. control structure, and continue. goes to the evaluation of B for the next item.
In the select. structure, the result R of T is compared to the elements of the result Ri of Ti , and the Bi block of the first case. or fcase. with a match is evaluated. Evaluation of the select. control structure then terminates for a case. , or continues with the next B(i+1) block for an fcase. (and further continues if it is an fcase.).
See NuVoc for more information on control structures.
<= =>
Primer Index Hover to reveal titles - Click to access - Current page is highlighted | ||||||||||||||||||||||||
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | ✅ |
26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | ✅ | ||||||
45 | 46 | 47 | 48 | ✅ | ||||||||||||||||||||
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | ✅ | ||||||||||||||||
59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | ✅ | |||||||
77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ✅ | |||||
97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | ✅ |