Php for loop continue vs break1/15/2024 Thus, the while-loop would have been the way to go. Loops were supposed to break on the loop condition and nowhere else. In slightly older times, I know breaking out of a loop was considered to be a no-no (on par with a goto statement). While ((i < MyList.In short, you should go with whichever version is the easiest to read and maintain. They both "structured goto (s)" sentences. NET (C# + VB), PHP, write your own) use "break" and "continue" to skip loops. It's a way to solve a problem, and there are other ways to solve it. You could probably even recommend it to your boss, even if he is the management rather than the CS type. It's also shorter and covers a narrower topic. I'm also gonna say here that Roberts' paper is far more accessible to the average programmer, so a better first read than Knuth's. Yes, you may be more experienced than CS101 students, but without using the break statement (or equivalently return/goto from the middle of loops), eventually you'll write code that while nominally being nicely structured is hairy enough in terms of extra logic variables and code duplication that someone, probably yourself, will put logic bugs in it while trying to follow your boss' coding style. That figure represents a success rate of less than 20%. Students who attempted to solve the problem without using an explicit return from the for loop fared much less well: only seven of the 42 students attempting this strategy managed to generate correct solutions. I have yet to find a single person who attempted a program using who produced an incorrect solution. For example, when a group of CS101-type students were asked to write code for a function implementing a sequential search in an array, the author of the study said the following about those students who used a break/return/goto to exit the from the sequential search loop when the element was found: Roberts summarizes several empirical studies conducted by others before him. Roberts titled Loop Exits and Structured Programming: Reopening the Debate ( ). What's far more important from a software engineering perspective is a more recent, 1995 paper by Eric S. (Just add a few extra variables?! Surely you can do that to please your boss.) But let's say that's just a purely theoretical result. Rao Kosaraju proved in 1973 is that it's not possible to rewrite all programs that have multi-level breaks of depth n into programs with break depth less than n without introducing extra variables. (This paper of Knuth was already recommended above by David Thornley, by the way.) What S. There's a computer science result called the Kosaraju's hierarchy of control structures, which dates back to 1973 and which is mentioned in Knuth's (more) famous paper on gotos from 1974. If Linus Torvalds were in your shoes, he would probably show your boss the middle finger! Your boss basically wants you to program in Pascal's control structures. Pascal (the programming language) was very bad especially for beginner programmers for that reason. But you may have to introduce additional variables and/or code duplication both of which typically make the program harder to understand. Yes you can write programs without break statements (or returns from the middle of loops, which do the same thing).
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |