PDA

View Full Version : Assignment 6 - Loops

07-28-2015, 02:59 PM
I recently completed this one. We had to make a character that sang the 99 bottles of pop song all the way down to 0. We were supposed to use a 'While' statement however I could not get that to work so I didn't use it and accomplished the same goal. If you did yours with the 'While' statement, I would like to know how you did it.:D

MrMoke
07-28-2015, 08:14 PM
Depending on the final verse either
While counter > 0
or
While counter >= 0
..sing
..dec the counter

CodeMan
07-28-2015, 09:04 PM
I recently completed this one. We had to make a character that sang the 99 bottles of pop song all the way down to 0. We were supposed to use a 'While' statement however I could not get that to work so I didn't use it and accomplished the same goal. If you did yours with the 'While' statement, I would like to know how you did it.:D

Just curious how did you do it?

Petit Suisse
07-30-2015, 03:42 AM
I'm also curious how you did it without a while statement. You're quite creative.

My solution:
http://gyazo.com/b08e6c0aa7b56d02a0fb3b09210ae065

08-06-2015, 12:58 PM
Just curious how did you do it?

I used a loop with a counter that incremented by 1 up to (but not including) 100 times

08-06-2015, 02:47 PM
I used a loop with a counter that incremented by 1 up to (but not including) 100 times

See attached...

08-06-2015, 02:51 PM
https://gyazo.com/a466644d1a0ce42fb1ecbdfa29e16f03

chickentree
08-06-2015, 04:37 PM
https://gyazo.com/a466644d1a0ce42fb1ecbdfa29e16f03

ndx = 99
while ndx > 0
ndx=ndx-1

stuff to do after the song has ended.

When you get to 1 it will sing about one bottle of soda and decrement ndx to 0 at which point the while loop will exit
Note: for testing I would make ndx=3 and make sure everything works then change it to 99 before you submit it.

Mark

noface0711
03-23-2016, 09:11 AM
I'm also curious how you did it without a while statement. You're quite creative.

RavenOfCode
03-23-2016, 04:54 PM
See attached...

I see, not the way I would do it but it works. :)

chickentree
03-25-2016, 10:38 PM
If Alice had a break statement, a programmer could write for loops that could be readily converted into a while loop or vice-versa.

Indeed this is true of all the "standard" looping constructs:

for
while
do ...while
repeat ... until
possibily others I can't think of.

With a few minor change these can all be used in any looping construct.
The reason to use one over another is because the chosen loop construct makes the code easier to understand, or the program's logic clearer.
In the context of the given assignment, the most natural choice, in my opinion, would be the for loop. I say this because the problem has a set number of loops and the for statement automagically gives the count needed to display the remaining number of bottles.
With a while loop the index must be created outside the loop and manually changed within the loop.
The only reason I can see to require using a while loop is to make sure the student understands how to use it and to compare the for and while loop structures.

Mark

P.S. If you can make it work with a for loop you've got all the pieces right and all that is left is to figure out the differences needed to make a while loop work.