Ian (Fluxtah) Warwick's blog RSS 2.0
# Thursday, March 05, 2009

Well, smack me with a wet kipper, I have never really considered the difference between do and do while, since I have always just used a while loop.

Anyway, the difference is, that a do-while will always execute before checking the condtion, whereas a while loop, checks the condition before executing the code block.

Quite a fundamental difference that could prove to be useful, I definetly see a 'gotcha!' interview question from this one if it was ever asked.

class Program
 {
     static void Main()
     {
         int x = 0; 

         // Will always output "Hello World"
         do
         {
             Console.WriteLine("Hello World");
             x++;
         } while (x <= 0); 

         // Will not output anything
         while (x <= 0)
         {
             Console.WriteLine("Hello World");
             x++;
         }
     }
 }
Thursday, March 05, 2009 4:40:00 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
C#
Archive
<March 2009>
SunMonTueWedThuFriSat
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Ian Warwick
Sign In
Statistics
Total Posts: 33
This Year: 0
This Month: 0
This Week: 0
Comments: 4
Themes
Pick a theme:
All Content © 2012, Ian Warwick
DasBlog theme 'Business' created by Christoph De Baene (delarou)