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

Jump statements - break, continue, return, and goto



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 used the break statement in switch statements,

where it helps in avoiding falling 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 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 above, we first create an array of int-type, numbers, with 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 a 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 the statement becomes true, resulting in the execution of the break statement. The break statement causes immediate termination of the for a 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 the 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 that 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.

In C#, there are several jump statements that can be used to control the flow of execution in a program. These include:

  • break: This statement is used to exit a loop or switch statement. When the break statement is encountered, the loop or switch is immediately terminated and control is transferred to the next statement following the loop or switch. For example:
for (int i = 0; i < 10; i++) { if (i == 5) { break; } Console.WriteLine(i); }

In this example, the loop will only execute five times and will print the numbers 0 to 4.

  • continue: This statement is used to skip the current iteration of a loop and continue with the next iteration. When the continue statement is encountered, control is transferred to the next iteration of the loop. For example:
for (int i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } Console.WriteLine(i); }

In this example, the loop will execute 10 times, but it will print only the odd numbers (1, 3, 5, 7, 9).

  • return: The return statement is used to exit a method and return a value (if the method returns a value). When the return statement is encountered, control is transferred back to the calling method and the value specified in the return statement is returned. For example:
int AddNumbers(int a, int b) { return a + b; }

In this example, the AddNumbers method returns the sum of the two arguments passed to it.

  • goto: The goto statement is used to transfer control to a labeled statement. When the goto statement is encountered, control is transferred to the labeled statement. For example:
int i = 0; start: Console.WriteLine(i); i++; if (i < 10) { goto start; }

In this example, the program will print the numbers 0 to 9 using a goto statement.

It is worth noting that goto statement is considered an old-fashioned and less readable way of doing things. It is usually better to use loops and conditional statements to control the flow of execution.

Related information

Sundar  Neupane

Sundar Neupane

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

If findandsolve.com felt valuable to you, feel free to share it.

Comments



Report Response