Question

I tried but the code wont work.

for.     T do. B end.
for_xyz. T do. B end.

What would be the equivalent of this from C#

for(int i = 0; i < 10; i++)
    Console.WriteLine("Hello World!");

And what's a good keyword to Google for J problems?

Was it helpful?

Solution

As noted at the beginning of the Control Structures section, these only apply within Explicit definition. The colon is the key for setting up such a script. The only time 'for.' (or any similar word) can occur is within a script determined by the right parameter to : , i.e. colon, meaning Explicit.

Use the link on the control-word for. on that page to find complete samples. Notice that these special symbols (such as for. and end.) normally occur in multi-line scripts that end with a single lone right-paren. That sort of structure is what you must use if you're to use control words.

Here is the first of the examples given on the Dictionary page documenting the for. structure (http://jsoftware.com/help/dictionary/cfor.htm):

f0=. 3 : 0
 s=. 0
 for. i. y do. s=. >:s end.
)

Once you have arranged control words inside this sort of structure, they take effect when the script is executed. In this example, when the verb f0 receives an integer as its only (right) parameter (referred to as y in the script) it results in the same integer. It iterates through the for loop to arrive at that number.

OTHER TIPS

A more J-ish way to loop is using Power ^:, this

f^:10 y

will apply f 10 times; first to y, then to f(y), ... :

f(f(f(f(f(f(f(f(f(f(y))))))))))

So if p is a print function, eg: p =: (4) 1!:2~ ]:

(p^:10) 'Hello World!'
Hello World!Hello World!...

In general J (in a way) promotes loop-less code. If you really needed 10 times the string 'Hello World!' for example, you probably would do something like:

10 12 $ 'Hello World!'
Hello World!
Hello World!
Hello World!
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top