• Have Any Question?
  • contact@eitworld.com

csharp jump statements





Jump Statements

 

  • Jump statements enable user to jump to another section of code either on the basis of any condition or without any condition.
  • Jump statement is basically used for the branching which causes the immediate transfer of the control of program to the other section of code.
  • These statements are also used where we need to interrupt any loop or switch-case statement in between the processing.
  • There are five Jump statement present in the C# as given below:
    • break
    • continue
    • goto
    • return
    • throw

Here we will only discuss the break, continue and goto statements. We will study the return statement in the chapter C# methodsand throw statement in the chapter C# Exception Handling. Now let discuss the other three statements one by one:

 

The break statement

 

    • The break statement terminates the closest enclosing loop or switch case statements and program’s control jump on the statement immediately following the loop or switch-case statement.
    • The syntax of break statement is:
break;

    • Here is an Example of break statement in which a loop is break after second iteration:
Example

 

using System;
namespace jump
{
class breakstatement
{
static void Main()
{
int i;
for (i = 1;
i <= 5;
i++)
{
Console.WriteLine(” “+i);
if (i == 2)
{
break;
}
}
Console.ReadLine();
}
}
}

The output of the above program will be:

 

Output

 

1
2

The continue statement

 

    • The continue statement forces the program control to go directly to the condition and then continue the next iteration. It means that continue statement stops the current iteration and starts the next iteration.
    • The syntax of continue statement is as follows:
continue;

    • Here is an Example of the continue statement which stop the second iteration and starts the third iteration:
Example

 

using System;
namespace jump
{
class continuestatement
{
static void Main()
{
int i;
for (i = 1;
i <= 5;
i++)
{
if (i == 2)
{
continue;
}
Console.WriteLine(” ” + i);
}
Console.ReadLine();
}
}
}

Here is the output of the above program:

 

Output

 

1
3
4
5

The goto statement

 

    • goto statement is a statement that is used when we want to jump on the specific section of code followed by a label.
    • The goto statement is most often used when there is a critical condition and programmer does not find the way to get outside the loop. It is widely used in nested loops.
    • The basic syntax of goto statement is as given below:
goto label;
…………
…………
label:
……..
……..

    • Here is an Example of goto statement in which we are going to the ‘end’ label at the ending of the loop:
Example

 

using System;
namespace jump
{
class gotostatement
{
static void Main()
{
int i;
for (i = 1;
i <= 5;
i++)
{
Console.WriteLine(” ” + i);
if (i == 5)
{
goto end;
}
}
end:
Console.WriteLine(“It is the end of loop”);
Console.ReadLine();
}
}
}

The output of the above program is given below:

 

Output

 

1
2
3
4
5
It is the end of loop



Real Time Web Analytics