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


C# return Statement

The return statement in c# will terminating execution of the current function, and transfer control to the calling function. It can be used perform conditional exit from a function to the main calling function.

protected void Page_Load(object sender, EventArgs e)
{
    printNumbers(6);
    Response.Write("Statement in the Main Calling Function");
}

void printNumbers(int nExitValue)
{
    for (int i = 1; i <= 10; i++)
    {
        if (i == nExitValue)
        {
            return;
        }
        //
        Response.Write(i.ToString() + "<br>");
    }
}
In the above example when i values is equal to 6 the return statement is reached, code execution is transferred to main calling function, the output in this case will be

1
2
3
4
5
Statement in the Main Calling Function


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

Related Post

C# goto Statement


C# goto Statement

The goto statement in c# is used to transfer control to a predefined label statement, it can be used to skip execution in a loop on reaching a condition, if a goto statement is reached then the code execution is transferred to the location where the goto label is pointing to.

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

In the above example when i values is equal to 5 the goto statement is executed and code execution is transferred to SkipLoop label statement, the output in this case will be

1
2
3
4
Statement after the for Loop


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

Related Post

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

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