×

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# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

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.

C# Tutorial

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

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# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

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# 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.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 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# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes 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# 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# 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# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.