From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,66ad83f28b84d875 X-Google-Attributes: gid103376,public From: Ted Dennison Subject: Re: about the exit; statment Date: 2000/03/22 Message-ID: <8baure$8t3$1@nnrp1.deja.com>#1/1 X-Deja-AN: 600912624 References: X-Http-Proxy: 1.0 x31.deja.com:80 (Squid/1.1.22) for client 204.48.27.130 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Wed Mar 22 17:11:25 2000 GMT X-MyDeja-Info: XMYDJUIDtedennison Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.7 [en] (WinNT; I) Date: 2000-03-22T00:00:00+00:00 List-Id: In article , =?ISO-8859-1?Q?Tim_Gahnstr=F6m?= wrote: > About the goto thread > Someone sugestet exit instead. > > I am studying right now and whe have been thought that in general exit > statments arent verry good either. > > (Though the Teacher have never even mentioned that the goto statment > exists!) My respect for your teacher grows with each post you make. :-) > Are these two statments a little equvivalent in terms of readability? What we are really talking about here is how easy it is for someone else (or yourself several months later) to figure out the control flow of your program. Looking at it this way, you should soon be able to make such judgments for yourself. For instance, a "for" loop is nice because you know without having to examine any other code exactly what code is being repeated and how many times (assuming no "exit" or "return" or "goto"). A while loop is a smidge harder to figure out, because the reader has to read the contents of the loop to figure out exactly what will cause an exit. A "loop" with a single "exit" is a bit worse than a "while" because now the code that gets repeated isn't so clearly defined; some of it gets repeated one time less than the rest. Of course sometimes this is exactly what you need to have happen. In that case an "exit" is probably the way to go. But it is tougher to understand that a "while" loop that doesn't have this behavior at all. A "loop" with multiple "exit"s or "returns" is worse still because there are multiple different ways the code could get repeated that the poor user has to figure them all out to understand what is going on. A "goto" is worst of all because potentially *any* code could get repeated. The user has to figure out for themselves if "goto" is used for a loop, or to exit from one. If it is used for a loop, there is no clear language-enforced limit to what code gets repeated, like the "end" statement is for normal loops. If its used to exit a loop, it could potientially carry the program's control anywhere, not just to the next statement after the loop as most other loop exit methods will do. Hopefully your teacher will be impressing upon you the importance of readability. It should be a very close second to getting it your code to work (and you will find they often go hand-in-hand). So really what we have here is a hierarchy of methods that can be used to direct program looping and control. In order of preference they are: for loop while loop middle or end-tested loop multiple-exited loop goto In any given situation you want to choose the one that is as high up the list as possible. The big catch is that the more powerful methods are lower on the list. If you actually need something more complex than a simple "for" loop you will have to rearrage the structure of things a bit to make it fit in a "for" loop. Of course there comes a point when that mangles things so badly that one of the more powerful loops like a "while" would actually be clearer. The principle applies all the way down the list. As a student you need to teach yourself where those lines are. Eventually it will become intuition, but at first you will probably find it useful to implement your loops in several different ways to see which is clearer. Don't be afraid to do this. In fact, when multiple exit loops are in question, I typically still do this. In my personal work I find that I use the top loop ("for") wherever possible. The top three constitute the majority of my loops. The second to last I use in special circumstances (typically searches) with copious comments. The last I have never used in 11 years in the industry. -- T.E.D. http://www.telepath.com/~dennison/Ted/TED.html Sent via Deja.com http://www.deja.com/ Before you buy.