FOLASAYO SAMUEL OLAYEMI
SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

SV_D_CURIOUS_SOFTWARE_ENGINEER'S BLOG

Difference between break and continue in c#

How to use break and continue statement in C#

FOLASAYO SAMUEL OLAYEMI's photo
FOLASAYO SAMUEL OLAYEMI
·Aug 21, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • break in c-sharp
  • continue in c-sharp

break in c-sharp

The break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. After terminating, the controls will pass to the statements that are present after the break statement, if available.

Code Sample

In the code sample below, jumps out of the loop when i is equal to 5:

using System;

namespace MyApplication

{
    class Program

    {
        static void Main(string[]args)

        {
            for (int i = 0; i < 10; i++)

            {
                if(i == 5)

                {
                    break;
                }
                Console.WriteLine(i);
            }
        }
    }
}

continue in c-sharp

The continue statement breaks one iteration in the loop, if a specified condition occurs, and continues with the next iteration in the loop.

Code Sample

The example below skips the value of 5:

using System;

namespace MyApplication

{
    class Program

    {
        static void Main(string[]args)

        {
            for (int i = 0; i < 10; i++)

            {
                if(i == 5)

                {
                    continue;
                }
                Console.WriteLine(i);
            }


        }
    }
}

Thanks for reading...

Happy Coding!

 
Share this