http://www.jsoftware.com/help/dictionary/ctrl.htm
for_i. y do. NB. will assign each item of y to i in loop. if. isPrime i do. i_index return. end. end. NB. _index is variable name appended to for_i in this case. ----- Original Message ----- From: Jon Hough <[email protected]> To: "[email protected]" <[email protected]> Cc: Sent: Sunday, April 27, 2014 11:36:00 AM Subject: [Jprogramming] Keeping a variable while running through a loop One problem I'm having with composing my own (non-trivial) J verbs is I am not sure how to hold onto a count variable when iterating through an array.For example: Problem: sum the number of primes less than 1000000. In Java-y syntax public int sumPrimes(){ int counter = 0; //my counter variable int max = 1000000; for(int i = 0; i < max; i++ ){ if(IsPrime(i)) counter += i; } return counter; } My first question is in J, how would I write a similar function/verb?Secondly, in that verb, how is "counter" stored as we iterate? (This question is not really about primes, but about holding onto variables through the iteration. I am aware of the p: and q: verbs) Regards,Jon ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
