Several types of jump statements in C #
Use the jump statement to execute a branch, which causes the program control immediately. Use the following keywords in jump statements: BreakContinuegarTurn // ------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------
BREAK statement terminates its nearest closed loop or Switch statement. Control is passed to the statement behind the termination statement (if any). The form of this statement is as follows:
Break;
Example In this example, the conditional 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 1234
Example This example demonstrates the usage of Break 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; cas 3: console.writeline ("current value is {0}", 3); break; default: console.writeline ("sorry, invalid selection."); Break;}}}
Enter 1
Example Output ENTER Your Selection (1, 2, or 3): 1Current Value IS 1
If you are input 4, the output is: Enter Your Selection (1, 2, or 3): 4Sorry, Invalid Selection.
/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -
Continue will control the next iteration of the closed iterative statement that is passed to it. Its form is:
CONTINUE;
Example In this example, the counter initials the count from 1 to 10. Skip between Continue and FOR tailings is skipped by using a CONTINUE statement (i <9). // statements_continue.csusing system; class contractSt {public static void main () {for (int i = 1; i <= 10; i ) {IF (i <9) Continue; console.writeline (i);}}}
Output 910
/ / -------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -
The GOTO statement passes the program control directly to the markup statement. Its form is one of the following:
Goto Identifier;
Goto Case Constant-Expression;
Goto Default;
among them:
Identifier
A label.
Constant-Expression
A Switch-Case tag.
Note
In the first form, Identifier indicates the label located in the current body, the same lexical range or goto-enclosed range. One of GOTO is usually used to pass control to a default label in a specific Switch-Case tag or Switch statement. The GOTO statement is also used to jump out of the deep nesting loop. If you have never referenced the label in the program, a warning message may be issued. For more information on tags, see 3.3 Declaration.
Example For an example of using GOTO to pass control to a specific Switch-Case tag, see Switch Example.
Example The following example demonstrates the use of GOTO jump out of the nested loop.
// statements_goto.cs // nested search loops
Using system; public class gototest1 {int x = 200, y = 4; int count = 0; string [,] myarray = new string [x, y];
// Initialize the array: for (INT i = 0; i // read INPUT: Console.write ("Enter the Number to Search for:"); // INPUT A STRING: STRING MyNumber = Console.readline (); // Search: for (INT i = 0; i 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.");}} Enter 44 Example Output ENTER THE NUMBER TO SEARCH for: 44the Number 44 is found.end of search. Example // statements_goto2.cs // CS0159 Expected // Labels Outside The Scope Using system; class unreachablecode {public static void main () {int x = 55; console.writeLine ("x = {0}", x); if (x == 55) {x = 135; goto a; // Error} x = x 1; for (int i = 1; i <= 5; i ) {a: console.writeline (i);} console.writeline ("x = {0}", x);}} In the previous example, the GOTO statement references tag A outside its range. The compiler will issue an error message: No Such label 'a' Withnin The Scope of the Goto Statement may issue a warning message due to never reference this label. If the label A is moved to the beginning of the For loop, the program will work normally, namely: a: for (int i = 1; i <= 5; i ) {// now the program compiles. / / -------------------------------------------------------------------------------------------- -------------------------------------------------- -------------------------------------------------- - RETURN statement terminates the execution of the method thereof and returns the control to the call method. It also returns the value of optional Expression. If the method is a Void type, the RETURN statement can be omitted. The form of this statement is as follows: Return [Expression]; Where: EXPRESSION is returned by the method. Expression does not work with the Void type method. Example In the following example, the A () method returns the AREA variable in the form of a Double value. // statements_return.cs Using system; class returntest {static double calculateArea (int R) {double area; area = r * r * math.pi; return area;} public static void main () {int RADIUS = 5; console.writeline ("The Area IS {0: 0.00} ", CalculateArea (RADIUS));}} Output The Area IS 78.54