Showing posts with label break. Show all posts
Showing posts with label break. Show all posts

Wednesday, May 23, 2012

C# break Vs continue Statement


C# break Vs continue Statement

The break statement in c# completely terminates the execution of the current loop, and processes the statement which is placed immediately after the loop.

The continue statement just skips the current iteration of the loop and processes the next iteration in the loop.

Break Statement
for (int i = 1; i <= 10; i++)
{
  
 if (i == 5)
   {
    
 break;
   }
  
 //
   Response.Write(i.ToString() + "<br>");
}
//
Response.Write("Statement after the for Loop");

Output
1
2
3
4
Statement after the for Loop

Continue Statement
for (int i = 1; i <= 10; i++)
{
   if (i == 5)
   {
     continue;
   }
   //
   Response.Write(i.ToString() + "<br>");
}
//
Response.Write("Statement after the for Loop");

Output
1
2
3
4
6
7
8
9
10

Statement after the for Loop 


That’s it, we have seen the difference between break and continue statements in c#


Related Post
C# break Statement
C# continue Statement
C# goto Statement
C# return Statement
C# break Vs continue Statement

C# break Statement


C# break Statement

The break statement in c# is used to terminate a loop, if a break statement is placed in a loop then the loop execution is terminated on reaching the break statement, control is transferred to the next executable statement which is placed after the loop.

for (int i = 1; i <= 10; i++)
{
   if (i > 5)
   {
     break;
   }
   //
   Response.Write(i.ToString() + "<br>");
}
//
Response.Write("Statement after the for Loop");

In the above example when I values is greater than 5 the break statement is executed and control in transferred to the next statement outside the loop, the output in this case will be

1
2
3
4
5
Statement after the for Loop


That’s it, we have evaluated the use of the break statement.



Related Post