Explanation of several jump statements in C #

Source: Internet
Author: User
Use a jump statement to execute a branch. This statement causes immediate transfer.ProgramControl. Use the following keywords in a jump statement:


Break
Continue
Goto
Return
//---------------------------------------------------------------------------------

BreakStatement to terminate the nearest closed loop or switch statement. Control the statements passed to the end statement (if any ). The statement format is as follows:
 
Break;
 
Example
In this example, the condition statement contains a counter that can be counted from 1 to 100, but the break statement terminates the loop after the count reaches 4.
 
// Statements_break.cs

Using system;


Class breaktest
{
Public static void main ()
{
For (INT I = 1; I <= 100; I ++)
{
If (I = 5)
Break;
Console. writeline (I );
}
}
}

 

 
Output
1
2
3
4
 
Example
This example demonstrates the break usage in the switch statement.
 
// Statements_break2.cs
// Break and switch
Using system;
Class Switch
{
Public static void main ()
{
Console. Write ("Enter your selection (1, 2, or 3 ):");
String S = console. Readline ();
Int n = int32.parse (s );
Switch (N)
{
Case 1:
Console. writeline ("current value is {0}", 1 );
Break;
Case 2:
Console. writeline ("current value is {0}", 2 );
Break;
Case 3:
Console. writeline ("current value is {0}", 3 );
Break;
Default:
Console. writeline ("sorry, Invalid selection .");
Break;
}
}
}
 
Input
1
 
Sample output
Enter your selection (1, 2, or 3): 1
Current value is 1
 
If 4 is input, the output is:
Enter your selection (1, 2, or 3): 4
Sorry, Invalid selection.
 
// Configure //-----------------------------------------------------------------------------------
 
ContinueThe next iteration of the closed iteration statement that passes the control to it. The format is as follows:
 
Continue;
 
Example
In this example, the counter is initialized to count from 1 to 10. By using the continue Statement (I <9) together with the expression, the statement at the end of the continue and for body is skipped.
// Statements_continue.cs
Using system;
Class continuetest
{
Public static void main ()
{
For (INT I = 1; I <= 10; I ++)
{
If (I <9)
Continue;
Console. writeline (I );
}
}
}
 
Output
9
10
 
//-----------------------------------------------------------------------------
 
GotoThe statement directly passes program control to the mark statement. It is in the form of one of the following:
 
Goto identifier;
 
Goto case constant-expression;
 
Goto default;
Where:
 
Identifier
 
A tag.
 
Constant-expression
 
A switch-case label.
 
Remarks
 
In the first form, identifier indicates the tag in the current body, the same lexical range, or the closed range of the GOTO statement.
A common usage of goto is to pass the control to a specific switch-case label or the default label in the switch statement.
The GOTO statement is also used to jump out of a deep nested loop.
If the tag has never been referenced in the program, a warning message may be sent. For more information about labels, see 3.3 declaration.
 
Example
For an example of passing control to a specific switch-case tag using Goto, see switch example.
 
Example
The following example demonstrates how to use goto to jump out of a nested loop.
 
// Statements_goto.cs
// Nested search Loops
 
Using system;
Public class gototest1
{
Public static void main ()
{
Int x = 200, y = 4;
Int COUNT = 0;
String [,] myarray = new string [x, y];
// Initialize the array:
For (INT I = 0; I <X; I ++)
For (Int J = 0; j <Y; j ++)
Myarray [I, j] = (++ count). tostring ();
 
// Read input:
Console. Write ("Enter the number to search :");
 
// Input a string:
String mynumber = console. Readline ();
// Search:
For (INT I = 0; I <X; I ++)
For (Int J = 0; j <Y; j ++)
If (myarray [I, j]. Equals (mynumber ))
Goto found;
 
Console. writeline ("the number {0} was not found.", mynumber );
Goto finish;
 
Found:
Console. writeline ("the number {0} is found.", mynumber );
 
Finish:
Console. writeline ("End of Search .");
}
}
 
Input
44
Sample output
Enter the number to search for: 44
The number 44 is found.
End of search.
 
Example
// Statements_goto2.cs
// Cs0159 expected
// Labels outside the scope
 using system; 
class unreachablecode
{< br> Public static void main ()
{< br> int x = 55;
console. writeline ("x = {0}", x);
If (x = 55)
{< br> X = 135;
goto; // error
}< br> X = x + 1;
for (INT I = 1; I <= 5; I ++)
{< br> A: console. writeline (I);
}< br> console. writeline ("x = {0}", x);
}< BR >}
In the previous example, the GOTO statement references tag a out of its range. The compiler will issue an error message:
No such label 'A' within the scope of the GOTO statement
Since this label has never been referenced, a warning message may also be sent.
If tag a is moved to the beginning of the for loop, the program will compile and run normally, that is:
A: For (INT I = 1; I <= 5; I ++) {// now the program compiles.
 
//------------------------------------------------------------
 
ReturnStatement to terminate the execution of the method in it and return the control to the calling method. It can also return the value of an optional expression. If the method is void type, you can omit the return statement. The statement format is as follows:
 
Return [expression];
 
Where:
Expression
The value returned by the method. Expression is not used with void methods.
 
Example
In the following example, the () method returns the area variable as a double value.
 
// Statements_return.cs
Using system;
Class returntest
{
Static double calculatearea (int r)
{
Double area;
Area = r * Math. Pi;
Return area;
}
Public static void main ()
{
Int radius = 5;
Console. writeline ("the area is {0. 00}", calculatearea (RADIUS ));
}
}
 
Output
The area is 78.54
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.