Net-informations.com
SiteMap  | About    

How to use C# do while loop

The C# while statement executes a statement or a block of statements until a specified expression evaluates to false . In some situation you may want to execute the loop at least one time and then check the condition. In this case you can use do..while loop.

The difference between do..while and while is that do..while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. From the following example you can understand how do..while loop function.




using System;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = 4;
            do{
                MessageBox.Show(" Loop Executed ");
                count++;
            }while (count < =4);
        }
    }
}

 CSharp Statements - Related Contents


 CSharp Statements - Related Programs