Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialolu adesina
23,007 Pointsi don't get it the user always wins
'Finally if all invaders are neutralized then the loop will'
4:20 'exit because remaining invaders will be zero.'
4:23 'Then we'll return true signifying that the user won.'
we will always reach true statement so does this mean the user always wins
3 Answers
andren
28,558 PointsAt around 4:02 the instructor writes an if statement that will be executed if one of the invader scores, and places a return false
statement inside. If that statement is reached then the function will stop, so the return true
statement outside the loop will never be reached.
frobe
1,368 PointsWhat never was really mentioned in the videos is that the return
keyword ends running the method / function (Level.Play()
in this case) immediately after returning the value following the return
keyword.
So, no more of the foreach
loop processing all _invaders
will run; also no more of the while
loop counting the remainingInvaders
will be processed: all loops exit (all code in the Method exits, actually) and false
is returned to the .Play()
Method.
In most of the examples so far the return
action was not nested in a loop, but located at the very end of the code of e.g. a Method. So we haven't learned this behavior yet.
kevin hudson
Courses Plus Student 11,987 PointsFrobe, surprisingly I understood everything you said :)