Up to Main Index Up to Journal for August, 2023 JOURNAL FOR THURSDAY 3RD AUGUST, 2023 ______________________________________________________________________________ SUBJECT: Going a little loopy… DATE: Thu 3 Aug 21:12:54 BST 2023 For the last several days I’ve been writing tests, examples and documentation on user functions for Mere ICE, nearly done, but I’m bored and need a break. So what does any deranged coder implementing their own programming language do? They experiment and go implement for-next loops of course! What else? :) >cat loopy.mr for x = 1; x <= 3; x++ println x next >mere loopy.mr 1 2 3 > The format is fairly standard. ‘for’ followed by initialisation, a condition and a post condition statement. A loop terminates at ‘next’. Everything after the ‘for’, apart from the semi-colons is optional. For example: >cat optional.mr x = 1 for ;; // infinite loop println x if x++ > 3; break next >mere optional.mr 1 2 3 > Is that a ‘break’ in there? Yes it is! There is a matching ‘continue’ too: >cat even.mr for x = 1; x <= 10; x++ if x % 2 == 1; continue println x next >mere even.mr 2 4 6 8 > "but what about…", nested loops? Got you covered there as well: >cat table.mr for x = 1; x <= 10; x++ for y = 1; y <= 10; y++ printf "%4d" x*y next println next >mere table.mr 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 > Although ‘break’ and ‘continue’ only work for the current for-next loop… for the moment :| Iterating arrays and maps? Hrm… okay… > cat arrayMap.mr a = []int 3 5 7 for x = 0; x < len a; x++ println x ": " a[x] next println m = [string] "a" "ant", "b" "bat", "c" "cat" for k = keys m; len k > 0; k = delete k 0 println k[0] ": " m[k[0]] next >mere arrayMap.mr 0: 3 1: 5 2: 7 a: ant b: bat c: cat > Tada! Okay, not quite so pretty :( I don’t have any automatic iteration like Go’s range yet. Give me a little slack, this is only a few hours coding… ;) Now I suppose I’d better get back to the boring stuff :( If you are very lucky I may, possibly, slip this into the next release — as a highly experimental feature sans tests and documentation… -- Diddymus Up to Main Index Up to Journal for August, 2023