Timeline for provability of while loop vs for loop
Current License: CC BY-SA 3.0
37 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 19, 2016 at 3:03 | comment | added | Arif Burhan | My preference is to use for when you know in advance the length, and while when you don't know the length in advance, eg. Processing a file or string provided by the user. However this is just a guide, you can use while( i++ < length ) instead of a for loop. | |
Apr 25, 2015 at 9:38 | comment | added | Tom Hawtin - tackline |
do-while loops are looked down upon. In a while loop the conditional expression is a precondition for the body and the negative of the loop postcondition. do-while is much more complicated to derive the conditions. You may as well use while even if you expect to always loop at least once.
|
|
Apr 25, 2015 at 8:17 | answer | added | Jai Thakur | timeline score: 0 | |
Apr 22, 2015 at 14:23 | comment | added | Per Alexandersson |
If you can prove that a while-loop terminates, you'd become a millionaire; while(non-trivial zero of zeta function not found) { search next interval ;}
|
|
Apr 22, 2015 at 11:49 | comment | added | Brandin |
@VincentAdvocaat I don't see why. I mean, it's pretty trivial to write a routine that converts a for loop into a while loop, right? So, assuming we had some proof of an algorithm that depended on there being a while loop there, then that's no problem, because we can also prove that the for loop (preferred by the professional programmer) is equivalent to the while loop (preferred by your prof)
|
|
Apr 22, 2015 at 11:26 | comment | added | Brandin |
I always thought of a for loop as being equivalent to a particular way of writing a while loop. Syntactical sugar. It doesn't change what the loop does, but it makes the physical act of writing and reading the loop easier.
|
|
Apr 22, 2015 at 11:19 | answer | added | RemcoGerlich | timeline score: 7 | |
Apr 22, 2015 at 8:06 | answer | added | Jörg W Mittag | timeline score: 3 | |
Apr 22, 2015 at 0:51 | comment | added | D.W. | Cross-posted on CS.SE: cs.stackexchange.com/q/41645/755 | |
Apr 21, 2015 at 20:03 | comment | added | Mike Nakis | It should also be noted that in order for it to be "trivially possible" for a checking tool to "statically prove" anything about a piece of code, then we can only be talking about trivial code. Trivial code might be necessary for life-critical or multi-billion-dollar-mission-critical applications, but it constitutes an infinitesimally small fraction of all code written today, so developing the skill to write trivial code does not have very good career prospects. | |
Apr 21, 2015 at 15:04 | history | tweeted | twitter.com/#!/StackProgrammer/status/590531640096587776 | ||
Apr 21, 2015 at 13:41 | comment | added | Vincent | @JeffO Because it only later came to me. | |
Apr 21, 2015 at 13:40 | comment | added | JeffO | This is the problem with education. Why didn't you ask your teacher?+ | |
Apr 21, 2015 at 13:22 | vote | accept | Vincent | ||
Apr 21, 2015 at 12:48 | answer | added | babou | timeline score: 13 | |
Apr 21, 2015 at 12:33 | vote | accept | Vincent | ||
Apr 21, 2015 at 13:22 | |||||
Apr 21, 2015 at 12:33 | comment | added | Vincent | @Doval exactly! | |
Apr 21, 2015 at 12:12 | comment | added | Doval |
The funny thing about the "all loops must have a fixed upper bound" rule is that for works more naturally for it, since you'll generally have some variable dedicated to terminating the loop.
|
|
Apr 21, 2015 at 11:11 | history | edited | Vincent | CC BY-SA 3.0 |
added 504 characters in body
|
Apr 21, 2015 at 9:38 | answer | added | mattnz | timeline score: -1 | |
Apr 21, 2015 at 9:23 | comment | added | Mike Nakis |
It does not boil down to that. That's someone's impression, and it is a false impression. Technically, for loops are equivalent to while loops, and in practice, you should never use a while loop there where a for loop could be used instead.
|
|
Apr 21, 2015 at 9:23 | answer | added | James Anderson | timeline score: -10 | |
Apr 21, 2015 at 9:11 | comment | added | Zaid Iqbal | In my opinion there is no difference but someone (like your teacher) can say that there is syntax complexity like: >For loop requires three arguments and while loop requires just one condition , so there is greater chances of mistakes in for loop than while loop. I know this is weird but it can be explained like that if someone really want to enjoy :) | |
Apr 21, 2015 at 9:08 | answer | added | milleniumbug | timeline score: 12 | |
Apr 21, 2015 at 8:59 | comment | added | Vincent | It is a fairly good teacher, but this is the only odd thing I've heard him say so far. it sounded plausible, that while loops could somehow be more reliable than for loops. at least that's what it boils down to i think. | |
Apr 21, 2015 at 8:57 | comment | added | sehe | @MikeNakis I don't know whether we have enough information to assess the mentaility of the teacher. | |
Apr 21, 2015 at 8:56 | comment | added | Mike Nakis | @sehe I am not referring to Vincent Advocaat as the cargo cult follower; I am referring to his teacher. | |
Apr 21, 2015 at 8:55 | comment | added | sehe | @MikeNakis on the contrary. A cargo culter wouldn't ask the question, just perpetuating myths. Perhaps even punishing others for not "adhering to the rule". | |
Apr 21, 2015 at 8:53 | comment | added | Mike Nakis | This is one of the best illustrations of cargo cult mentality that I have come across in recent times. | |
Apr 21, 2015 at 8:50 | history | edited | Mike Nakis | CC BY-SA 3.0 |
formatting, mostly.
|
Apr 21, 2015 at 8:44 | history | edited | yannis |
edited tags
|
|
Apr 21, 2015 at 7:00 | comment | added | Nathan | For(;;) { cout<<"they're really not that different" <<endl;} . The biggest difference is that for loops let you easily keep a variable (i) at the scope of the loop. | |
Apr 21, 2015 at 6:59 | comment | added | Vincent | @KilianFoth while over for (not for over while). might be i misinterpreted him but he was quite clear about a while loop being provable and a for loop not (or less) | |
Apr 21, 2015 at 6:59 | comment | added | user7043 |
I agree with Kilian. It makes no sense that there should be a difference, as for (init; test; step) { body } has a trivial desugaring to while : {init; while (test) { step; body }} .
|
|
Apr 21, 2015 at 6:52 | comment | added | Kilian Foth |
Your teacher is either completely wrong, or you have misreported what he said somehow. This rationale for preferring for over while simply makes no sense. There may be one, but this isn't it.
|
|
Apr 21, 2015 at 6:51 | review | First posts | |||
Apr 21, 2015 at 7:54 | |||||
Apr 21, 2015 at 6:49 | history | asked | Vincent | CC BY-SA 3.0 |