Timeline for How to write correct loops?
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 26, 2018 at 21:50 | comment | added | usr | This is actually the most practical answer. The testing answers do not provide a way to simply write a correct loop on the first try. I tend to plug in values to the loop iteration variables, not so much to the boundary expressions. | |
| Apr 18, 2016 at 19:44 | comment | added | user | @porglezomp Ah, yes, the goes to operator. Most C-like languages, including C, C++, Java and C#, has that one. | |
| Apr 18, 2016 at 2:26 | comment | added | porglezomp | @BryceWagner if you need to do i-- > 0, why not try out the classic joke, i --> 0! | |
| Apr 18, 2016 at 1:58 | comment | added | TOOGAM | Naturally, if using a language that requires " > 0 " to get desired functionality, then such characters should be used because they are required by that language. Still, even in those cases, using just the " > 0 " is simpler than doing the two-part process of first subtracting one, and then also using " >= 0 ". Once I learned that through a bit of experience, I got into the habit of needing to use the equal sign (e.g., " >= 0 ") in my loop test conditions far less frequently, and resulting code has generally felt simpler since. | |
| Apr 17, 2016 at 23:43 | comment | added | Bryce Wagner | @TOOGAM (int i=length; i--; ) works in C/C++ because 0 evaluates as false, but not all languages have that equivalence. I guess you could say i-- > 0. | |
| Apr 17, 2016 at 19:19 | comment | added | TOOGAM | I really like for (int i = 0; i < length; i++). Once I got into that habit, I stopped using <= nearly as often, and felt that loops got simpler. But for (int i = length - 1; i >= 0; i--) seems overly complicated, compared to: for (int i=length; i--; ) (which would probably be even more sensible to write as a while loop unless I was trying to make i have a smaller scope/life). Result still runs through loop with i==length-1 (initially) through i==0, with only functional difference being that the while() version ends with i== -1 after the loop (if it lives on), instead of i==0. | |
| Apr 17, 2016 at 14:42 | history | edited | Bryce Wagner | CC BY-SA 3.0 | edited body |
| Apr 17, 2016 at 14:38 | review | First posts | |||
| Apr 19, 2016 at 10:32 | |||||
| Apr 17, 2016 at 14:34 | history | answered | Bryce Wagner | CC BY-SA 3.0 |