find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Jump Statements (The break Statement and The Continue statement)



As the name suggests,jump statements allow you to jump from the current statement to another statement in your program.Jump statements are mainly used with loops and switch statements.In loops,you can use jump statements to break out of a loop or to skip the statements after a particular statement in the loop. C# provides the following two jump statements:

  • The break Statement
  • The Continue statement
   Using the break statement
 We have already use the break statement in switch statements,

where it helps in avoiding fall through.However,it can also be used in loops to terminate or stop the execution of the loop.This implies that the execution of the loop stops immediately and the control passes to the statement that follows the loop.If the loop has statements after the break statement,then all those statements are skipped.In nested loops,the break statement terminates only the current loop,that is,the loop in which is used.For example,if then only the inner for loop is terminated without affecting the outer for loop.

The syntax of the break statement is as follows:
break;

In the preceding syntax,
break: Refers to the keyword is used to indicate a break statement.

using System;
public class Program
{
public static void Main()
{
int[] numbers={1,2,3,4,5,6,7,8,9,10};
for(int i=0;i<numbers.Length;i++)
{
if(numbers[i]==6)
break;
Console.WriteLine("Numbers["+i+"]"+"="+numbers[i]);
}
Console.ReadLine();
}
}

In the code of above,we first create an array of int type,numbers,with the numbers from 1 to 10.After this,we use a for loop to display the array elements on the console.In the loop,the local variable,i,is used as an index for the numbers array to iterate through the array elements.The for loop contains an if statement,which checks whether the current array element has the value,6 .The condition in the if statement evaluates to false for the first five iterations of the for loop.Therefore,the for loop executes displaying the values of those five array elements.However,in the sixth iteration of the for loop,the condition in the if statement becomes true,resulting in the execution of the break statement.The break statement causes immediate termination of the for loop.


Using the continue Statement

The continue statement causes immediate termination of the current iteration of the loop in which it is used.It does not cause termination of the entire loop. The continue statement passes the control to the next iteration of the loop. If there are any statements that are written after the continue statement,then those statements are skipped.
 continue;

In the preceding syntax,
continue : Refers to the keyword indicates a continue statement.

using System;
public class Program
{
public static void Main()
{
for(int i=0;i<=5;i++)
{
Console.WriteLine("Iteration" + i + "begins");
if(i==4)
continue;
Console.WriteLine("Iteration "+ i + "ends");
}
Console.ReadLine();
}
}

In the code above, a for loop is used to iterate a set of statements five times .Inside the for loop, two statements are used that display two messages indicating the start and end of the current iteration on the console.An if statement is used between these two statements in order to check if the current value of the loop control variable,i,is 4,and also a continue statement is written immediately after the if condition.The continue statement will be executed only if the condition in the if statement evaluates to true.This implies that during the fourth iteration,the statement that indicates the end of the current iteration is not executed.


Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response