Sponsored Ad

Tuesday, May 11, 2010

How Break Statement works in loop – C# | Break Statement Example

Break statement breaks the loop and sends the control pointer to out of the loop. Neither it executes the after break code nor it complete the rest of the loop.

As you can see in example after break code never executes and before break only executes once.

 

Output:

Break Example Output C#

 

Source code:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.BreakExample();
            Console.ReadLine();
        }

        private void BreakExample()
        {
            for (int i = 1; i <= 5; i++)
            {
                WriteLine("Before Break : " + i);
                break;
                WriteLine("After Break : " + i);
            }
        }
    }
}

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers