Search Placementyogi.com
 

break and continue in C#

break statement in C#

The break statement is used to exit from the current block. It can be used at three places:

  1. To exit from the switch block
  2. To exit a for loop
  3. To exit a while loop

 

1. Using break to exit from the switch block

The switch case is completely based on break statements. 

int i=1;
switch(i)
{
	case 1: System.Console.WriteLine("i=1");
			break;
	case 2: System.Console.WriteLine("i=2");
			break;
	default: System.Console.WriteLine("default case");
			break;
}

If the case matches, the statements inside the matching case get executed and then break takes the control out of the switch.

If we do not use the break statement, we get compiler error saying that "Control cannot fall through from one case to another".

2. Using break to exit a for loop

The break statement when encountered inside a loop , takes the control out of the for loop and the next statement after the loop is executed.

for (int i=0;i<10;i++)
{
	if(i==4)
		break;
	System.Console.WriteLine("i="+i);
}
System.Console.WriteLine("Outside the loop");

3. Exiting while loop using break:

int i=0;
while (i<10)
{
	if (i==4)
		break;
	System.Console.WriteLine("i="+i);
	i++;
}
System.Console.WriteLine("Outside the loop");

Output:

i=0

i=1

i=2

i=3

Outside the loop

4. Using break inside a nested loop:

When break is used inside a nested loop, the control only comes out of the inner loop.

Example:

for(int i=0;i<5;i++)
{
	for(int j=0;j<5;j++)
	{
		if (j==2)
			break;
		System.Console.WriteLine("i="+i+", j="+j);
	}
}

Output:

i=0, j=0

i=0, j=1

i=1, j=0

i=1, j=1

i=2, j=0

i=2, j=1

i=3, j=0

i=3, j=1

i=4, j=0

i=4, j=1

Here, in the inner loop, when the value of j equals 2, the inner loop breaks. It does not affect the outer loop.

Try to trace the above program and understand the control flow.

Continue statement in C#

continue is used when we have to skip the remaining portion of the code in the current iteration but continue running the loop from the next iteration.

continue shifts the control to the beginning of the block i.e. in this case the "iteration" part of the for statement.

Example:

for(int i=0;i<5;i++)
{
	if (i==2)
		continue;
	System.Console.WriteLine("i="+i);
}

Output:

0

1

3

4

 

2 is not printed because as soon as i becomes true, continue is executed and the remaining portion of the code is skipped and i++ is executed and the next iteration starts executing.

1. continue inside a nested loop

When continue is used inside a nested loop, only the inner loop's current iteration is skipped and the outer loop is not affected at all.

public class ForLoop
{
    public static void main(String[] args)
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (j == 2)
                    continue;
                System.Console.WriteLine("i=" + i + ", j=" + j);
            }
        }
    }
}

Output:

i=0, j=0

i=0, j=1

i=0, j=3

i=0, j=4

i=1, j=0

i=1, j=1

i=1, j=3

i=1, j=4

Notice: j=2 is not printed in any of the iteration.

2. Using continue with while loop

As we know, continue skips all the remaining statements inside the current iteration and goes to the beginning of the block.

Consider the following program:

public class WhileLoop
{
	public static void main(String[] args)
	{
		int i=10;
		while(i<20) //line6
		{			
			if(i==11)
				continue;
			System.Console.WriteLine("i="+i);
			i++;
		}
		System.Console.WriteLine("Last statement inside main");
	}
}

Output: 

It prints i=10 and then goes into an infinite loop.

Lets look into this closely, 

i is 10 initially, then the control enters the loop.

i==11 is false, so i=10 is printed and i is incremented to 11.

Now, i is 11 and so continue is executed, it skips everything and goes to the  while statement at line6. 

Notice that i++ was not executed because of the skip, Hence the value of i is still 11.

if(i==11) again evaluates to true and continue is executed.

This goes on and so the program runs into an infinite loop! Damn!

 

 

 

 

Banner to LogicGuns.com

Hide Page Information

C#.NET Tutorial - Break and Continue | Example and Syntax with Explanation

This tutorial will help freshers to understand the meaning of break and continue statements in c#.net with real world examples and explanation along with programs that will help beginners in c# to grasp the concept of break and continue in c#.net.