Welcome to the Treehouse Community
Looking to learn something new?
Brian Platt5,009 Points
Loops in real life
Hey, So I just finished learning about loops in PHP (but to my understanding it's a pretty universal coding concept) and just wanted to get a feel for how they work in real life. Is this like when we take code challenges here on Treehouse and we see the "Bummer..." text if we miss a question? Do the programmers put every possible correct answer as a 'elseif' statement at the end of the loop and you can only proceed if you input one of those? Or is this all beyond where I am? Thanks!
Loops have an almost infinite amount of uses. A loop is useful for any sort of task that requires iteration.
Is this like when we take code challenges here on Treehouse and we see the "Bummer..." text if we miss a question?
No. If there is an actual error with the loop, the code will break and the compiler will let you know, if there is one. But more often than this you'll run into logic errors, where the code runs fine but it isn't exactly doing what you intended for it to do. You can create issues that you don't realize are issues until months down the road. So in real life, there isn't always something to tell you that you're doing something wrong. There are practices and methods to combat this, which you will eventually learn.
Do the programmers put every possible correct answer as a 'elseif' statement at the end of the loop and you can only proceed if you input one of those?
It sounds like you're referring to a very specific loop / set of conditions. The programmer will make the exact amount of 'elseif' statements the program requires, there really isn't a set standard. For when the possible conditions are too great in number, then there are other ways of going about it without cluttering your code, but any method will essentially do the same thing. I don't think it's terribly common to have that many conditions for one statement, though. Remember that the uses of these tools, such as loops and conditions, are basically infinite. Existing loops and conditions that you'll run across will come in all sorts of different shapes, sizes, and configurations.
You're still learning the concepts of how programming works. These sort of concepts will become much clearer to you as you continue learning. Try not to concern yourself with how coding works in "the real" world for now, focus on learning the tools and how you can use these tools to make the program do what you want. That is essentially real programming.
Keep learning, and good luck!