C# Basics

C# Jump Statements

You may need to use C# jump statements to control the order in which the statements are executed.Three most commonly used jump statements are:


C# break Statement

The break keyword causes the program to exit a loop prematurely before it has completed its execution. A loop’s termination with the break operator can only be done from its body during an iteration of the loop. When break is executed, the code in the loop’s body is skipped, and the control is passed to the first expression immediately following the loop.

C# break statement flow diagram

C# break statement flow diagram
C# for loop break statement

Note: break is used with the loop or switch statement.

You cannot use the break command to exit both loops simultaneously. Two separate break commands must be used—one for each loop. A break statement within a nested loop breaks out of the inner loop only.

In the following example, the break statement exits loop B and returns control back to loop A.

C# Nested loop break statement

In the below code, notice that the loop ends prematurely at the break statement when the counter reaches 3 == 3. Without the break keyword, the loop should run from 0 to 4 because the loop condition is counter <= 4. However, with the break keyword, when counter = 3, the condition marked A evaluates to true. The break keyword then causes the loop to end prematurely and resumes execution immediately after the loop.

for (int counter = 1; counter <= 4; counter ++)
{
  if (counter == 3) // -> A
    break;

  Console.WriteLine(counter);
}

Run Demo

OUTPUT

1
2


C# while Loop

C# while loop break statement

When the counter reaches 3, the condition marked A evaluates to true. The break keyword then causes the loop to end prematurely and resumes execution immediately after the loop.

int counter = 0;

while (counter < 4)
{
  counter++;
  if (counter == 3) // -> A
    break;

  Console.WriteLine(counter);
}

Run Demo

OUTPUT

1
2


C# do-while Loop

C# do-while loop break statement

When the counter reaches 3, the condition marked A evaluates to true. The break keyword then causes the loop to end prematurely and resumes execution immediately after the loop.

int counter = 0;

do
{
  counter++;
  if (counter == 3) // -> A
    break;

  Console.WriteLine(counter);

} while (counter < 4);

Run Demo

OUTPUT

1
2


C# foreach Loop

C# foreach loop break statement

This code writes out the numbers 1 and 2 because the break command causes the loop to exit when the value reaches 3.

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
  if (value == 3) // -> A
    break;

  Console.WriteLine(value);
}

Run Demo

OUTPUT

1
2


C# continue Statement

Another commonly used jump statement is the continue statement. When we use continue, the rest of the current iteration is skipped after the keyword, and the execution of the next iteration is immediately initiated. In other words, it passes control straight back up to the conditional expression at the top of the loop to start over.

C# continue statement flow diagram

C# continue statement flow diagram

C# for Loop

C# for loop continue statement

Note: continue is used with the loop.

First, we initialized the counter variable to run from 1 to 4. When counter = 3, the condition marked A evaluates to true. This causes the for loop to skip the rest of the current iteration and initiates the execution of the next iteration.

for (int counter = 1; counter <= 4; counter++)
{
  if (counter == 3) // -> A
    continue;

  Console.WriteLine(counter);
}

Run Demo

OUTPUT: 3 skipped

1
2
4


C# while Loop

C# while loop continue statement

In the below code sample, all of the numbers will be printed out except one. 3 gets skipped because of the continue statement. When it hits condition marked A, it jumps back up and continues on with the next cycle through the loop.

int counter = 0;
while (counter < 4)
{
  counter++;
  if (counter == 3) // -> A
    continue;

  Console.WriteLine(counter);
}

Run Demo

OUTPUT: 3 skipped

1
2
4


C# do-while Loop

C# do-while loop continue statement

When a counter reaches 3, the condition marked A evaluates to true. This causes the do-while loop to skip the rest of the current iteration and initiates the execution of the next cycle due to continue statement.

int counter = 0;

do
{
   counter++;
   if (counter == 3) // -> A
    continue;

   Console.WriteLine(counter);

} while (counter < 4);

Run Demo

OUTPUT: 3 skipped

1
2
4


C# foreach Loop

C# foreach loop continue statement

First, we initialize an array and assign value to it. When value = 3, the condition marked A evaluates to true. This causes the foreach loop to skip the rest of the current iteration and initiates the execution of the next iteration.

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
  if (value == 3) // -> A
    continue;

  Console.WriteLine(value);
}

Run Demo

OUTPUT: 3 skipped

1
2
4


C# goto Statement

There is another jump statement called the goto statement, which enables you to jump directly to another specified line in the program indicated by a label. It is important that this keyword be used in a very limited manner, and its usage must be avoided wherever possible because it makes a program that’s difficult to read and maintain.

Syntax

goto identifier;

Label is a placeholder in a code block that has a colon suffix “:” in front of it.

C# goto statement

When used within a switch statement:

Syntax

goto case const-expression;
goto default;
C# switch goto statement

Run Demo

OUTPUT

Case: C


Just for Fun

  goto One;
  Console.WriteLine("This won't execute");

One:
  Console.WriteLine("1");
  goto Two;

Four:
  Console.WriteLine("4");
  goto Total;

Two:
  Console.WriteLine("2");
  goto Three;

Three:
  Console.WriteLine("3");
  goto Four;

Total:
  Console.WriteLine("——");
  Console.WriteLine("10");

Run Demo

OUTPUT

1
2
3
4
——
10


Iteration Statements

There are four looping structures in C# where you can use jump statements. Read more on …


C# Reference | Microsoft Docs