>>  <<  Ndx  Usr  Pri  Phr  Dic  Rel  Voc  !:  wd  Help  Primer

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.

See the J Dictionary for more information on control structures.


>>  <<  Ndx  Usr  Pri  Phr  Dic  Rel  Voc  !:  wd  Help  Primer