Talk:Loops/N plus one half: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 8: Line 8:


A more correct name might be '''"Loop/n minus one half"''', but even better might be '''"Looping with a different action on the last iteration"''' - its not as twee, but more accurate? --[[User:Paddy3118|Paddy3118]] 06:10, 5 November 2009 (UTC)
A more correct name might be '''"Loop/n minus one half"''', but even better might be '''"Looping with a different action on the last iteration"''' - its not as twee, but more accurate? --[[User:Paddy3118|Paddy3118]] 06:10, 5 November 2009 (UTC)

: Actually it turns out I mis-remembered the name of the loop pattern. The correct name seems to be: "Loop and a half".
: "Looping with a different action on the last iteration" would be wrong (and actually pointless, because then the natural thing to do would be to just do the different thing after the loop). After all, the action to do is the same as the action on previous loops, except that on the last iteration not ''all'' of the loop body is executed. --[[User:Ce|Ce]] 17:25, 5 November 2009 (UTC)

Revision as of 17:25, 5 November 2009

I noticed that many implementations don't run the loop 9.5 times. Instead a check is made whether the comma has to be printed. This way the loop still runs exactly 10 times, with a different action taken depending on the loop variable.

This is pointed out in the description:
in the last iteration one executes only part of the loop body
Notice the quotes around n+1/2 showing that it's not literally n+1/2 times, but rather the last time through only part of the work is done. Running the loop 9.5 times isn't a requirement, but if you can get your language to do it then go for it. --Mwn3d 21:09, 4 November 2009 (UTC)

Task name doesn't make sense?

You are asked to print the numbers one to ten with separating commas. Surely n == 10. And so you want to go through a loop doing the same thing n minus one, i.e 9 times; then do something else the n'th time through the loop, as many of the examples do.

A more correct name might be "Loop/n minus one half", but even better might be "Looping with a different action on the last iteration" - its not as twee, but more accurate? --Paddy3118 06:10, 5 November 2009 (UTC)

Actually it turns out I mis-remembered the name of the loop pattern. The correct name seems to be: "Loop and a half".
"Looping with a different action on the last iteration" would be wrong (and actually pointless, because then the natural thing to do would be to just do the different thing after the loop). After all, the action to do is the same as the action on previous loops, except that on the last iteration not all of the loop body is executed. --Ce 17:25, 5 November 2009 (UTC)