Q:

What is the difference between “continue” and “break” statements in C#?

0

What is the difference between “continue” and “break” statements in C#?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

break statement:

The break statement terminates the execution of the nearest enclosing loop. After termination of the loop or switch body, control passes to the statement that follows the terminated statement.

Flowchart of break:

using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace break_example
{
Class brk_stmt
{
    public static void main(String[] args)
    {
        for (int i = 0; i <= 5; i++)
        {
            if (i == 4)
            {
                break;
            }
            Console.WriteLine("The number is " + i);
            Console.ReadLine();
        }
    }
}
}

Output:

The number is 0;
The number is 1;
The number is 2;
The number is 3;

continue statement:

We can terminate an iteration without exiting the loop body using the continue keyword. When continue (jump statement) execute within the body of the loop, all the statements after the continue will be skipped and a new iteration will start. In other words, we can understand that continue causes a jump to the end of the loop body.

Flowchart of continue:

using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace continue_example
{
Class cntnu_stmt
{
    public static void main(String[] {
        for (int i = 0; i <= 5; i++)
        {
            if (i == 4)
            {
                continue;
            }
            Console.WriteLine(“The number is "+ i);
                              Console.ReadLine();
        }
    }
}
}

Output:

The number is 1;
The number is 2;
The number is 3;
The number is 5;

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What can you tell us about the XSD file in C#?... >>
<< What are delegates?...