×

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used inside the for loop and switch statements. If you are using nested loop into your program then break statement will break only inner loop. You can’t use break outside any loop or switch statements . Let's see a simple example of break statement:
using System;
public class BreakExample{
            public static void Main (string[] args)
            {
                        int i;
            for(i=0;i<10;i++)
            {
                        Console.WriteLine(i);
                        if(i==6)
                                    break;
            }
                        Console.WriteLine("loop break i="+i);
            }
}
Output
0
1
2
3
4
5
6
loop broke i=6
C# Continue statement: Continue statement is used to skip the remaining code of the loop for a specified condition and continue the flow control from next iteration. It is used if you don’t want to run a certain part of loop for a separate condition. Let's see a simple example of continue statement
using System;
public class ContinueStatementExample
{
public static void main (string[] args)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine(i);
If(i==5) 
Continue;
}
}
}
Output
1
2
3
4
6
7
8
9
10
C# goto statement: Goto statement is also known as a jump statement. It transfers the flow of control somewhere else in the program (where the label is mentioned). It is mainly used to break deeply nested loop or nested switch statements. Using goto statements is mainly avoided since it increases program complexity.
C# goto statements Example:
using System;  
public class GotoExample  
{  
public static void Main(string[] args)  
{  
ineligible:  
Console.WriteLine("You are not eligible to vote!");  
Console.WriteLine("Enter your age:\n");  
int age = Convert.ToInt32(Console.ReadLine());  
if (age < 18){  
goto ineligible;  
}  
else  
{  
Console.WriteLine("You are eligible to vote!");   
}  
}  
}
Output
You are not eligible to vote!
Enter your age:
17
You are not eligible to vote!
Enter your age:

Related Topics

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.