Path: utzoo!mnetor!uunet!mcvax!enea!sommar From: sommar@enea.se (Erland Sommarskog) Newsgroups: comp.lang.misc Subject: FOR loops Message-ID: <2853@enea.se> Date: 15 Mar 88 22:21:09 GMT References: <2827@enea.se> <1557@pasteur.Berkeley.Edu> Reply-To: sommar@enea.UUCP(Erland Sommarskog) Followup-To: comp.lang.misc Organization: ENEA DATA AB, Sweden Lines: 53 Wayne A. Christopher (faustus@ic.Berkeley.EDU) writes: >In article <2827@enea.se>, sommar@enea.se (Erland Sommarskog) writes: >> ... Ada as I see it have >> the perfect solution: The loop variable is declared in the FOR- >> statement, and is thus not accessible afterwards. > >Here's the problem with that construct: I often write > for (i = 0; i < max; i++) > if (something) > break; > if (i == max) > ... >If the loop variable is inaccessible outside of the loop there's >no way to tell how it terminated, except by using an extra flag, >which is ugly. Sorry to be a little rude, but is just an argument for the Ada style. I find your style ugly and Ada stops you using it. Why is it ugly? Because you're using i in two ways here. First as an iterator, and the as a flag to se if "something" (dit not) occur. So the extra flag is not ugly. Another point is that I would probably use a WHILE loop instead. Some proposals: (in Ada) -- WHILE a la Pascal i := 0; While i < max and not something loop ... End loop; -- Here you *can* test use i for the test, but I'd prefer "something" -- FOR loop with exit-statement. For i in 0..max - 1 loop -- And why not 1..max? But we have had that debate. Exit when something; End loop; If not something then -- Or if "something" is an error condition, use an exception handler Begin For i in 0..max - 1 loop End loop Do_the_normal_stuff; Exception when Something => Clean_up_or_re_raise; End; Continue_if_you_like; -- Erland Sommarskog ENEA Data, Stockholm sommar@enea.UUCP "Si tu crois l'amour tabou... Regarde bien, les yeux d'un fou!!!" -- Ange