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 trial
Chris Ward
12,129 PointsConditional selection of loops
A useful feature missing from many programming languages is conditional selection of loops. This feature is really a marriage between if, else-if, else and loops. And while you could get the same effect using if, else-if, else statements, it seems cumbersome.
Currently, if you wanted loop selection behavior, you would have to write:
if (condition 1) { while (condition 1) { ... } } else if (condition 2) { while (condition 2) { ... } } else if (condition 3) { while (condition 3) { ... } } .... { } else { do { ... } until (condition n); }
It's much simpler to write:
while (condition 1) { ... } else while (condition 2) { ... } else while (condition 3) { ... } else do { ... } until (condition n); }
So, what do you think of this while, else-while, else-do-until loop selection idea? It is certainly much simpler to write, but do you think it has widespread practical use?
2 Answers
Gunhoo Yoon
5,027 PointsYour loop condition should be sufficient to cover that. Those seem like pretty unnecessary features to me.
So this is what you feel uncomfortable doing it?
if (A)
{
while(A) {...}
}
else if (B)
{
while(B) {...}
}
else
{
while(C) {...}
}
My question is why do you need to check the same condition twice for both if and while?
Chris Ward
12,129 PointsLet me clarify, this would be like selecting one loop condition from among many...maybe it is better to say: if while(condition) {...} else if while(condition) {...} else if while(condition) {...} else do {...} until(condition); which only enters one loop EVER in order to distinguish it from the also useful (now revised) while(mutually exclusive condition) {...} else while(mutually exclusive condition) {...} else while(mutually exclusive condition) {...} else do {...} until(condition); where one while loop is selected until all of the mutually exclusive conditions are exhausted.
Gunhoo Yoon
5,027 PointsUnless you are doing things in extremely procedural way I don't see the point. Maybe post a examplary code that would benefit from your suggestion?