Showing posts with label continue. Show all posts
Showing posts with label continue. 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# continue Statement


C# continue Statement

The continue statement in c# is used to skip the current iteration of a loop, if a continue statement is placed in a loop then the current iteration of the loop execution is skipped on reaching the continue statement, and the loop progresses to the next iteration.

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

In the above example when i values is equal to 5 the continue statement is executed and the current iteration is skipped, control in transferred to the next iteration of the loop, the output in this case will be
1
2
3
4
6
7
8
9
10

Statement after the for Loop

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



Related Post