C#: What are Loops? [10 of 19]

Play C#: What are Loops? [10 of 19]

The Discussion

  • User profile image
    Scott and Kendra Nice Explanation of Loops.
    But I have a question when ,where and why should use while,do while loop.
  • User profile image

    While loops are often used to run an application loop or game loop. By that I mean, when you start a game, it is continuously looping to do the various things take place in a game until you enter a command to exit. This is everything from accepting input, checking how far a character moved in how much time, playing music, etc. It doesn't necessarily make sense to use a standard for loop to control this game loop as your game isn't incrementing / decrementing a certain amount of times then exiting. It's just running until something tells it to exit. "The game will continue to run while escape is not pressed". I use a game as an example here but even the most simplest of console apps can use a while loop to run.

    For loops are heavily used to go over collections of things, for example, a list of people. They are the backbone for a lot of the looping mechanisms that might be abstracted away from you. Meaning, you might not write out the full for loop syntax but it's probably "happening behind the scenes" in something the language provides to you.

    Do while is like a while but used when you know your loop is ALWAYS going to run at least once. There are definitely cases for it but it's not as commonly used as while or for.

  • User profile image
    You can tell Scott gets excited enough to move off script. Makes sense - VB is becoming more and more amazing after each and every update.
  • User profile image
    The while-loop feels all over the place. The for-loop has a better look and can be spotted faster.

    One thing is that the for-loop will always execute depending on the condition. the do-while loop in other-hand is going to execute the code regardless of the condition at least once.

Add Your 2 Cents