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

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

The Discussion

  • User profile image
    tahirhussai​n858
    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
    Dephora
    @tahirhussain858:

    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.

Add Your 2 Cents