C # refer to the jump Statement (break, continue, Goto, return, throw)

Source: Internet
Author: User

Timely and effective jump will help improve program execution efficiency ----------------------------------------------------------- the break statement is used to terminate the recent closed loop or the switch statement where it is located. Control the statements passed to the end statement (if any ). The continue statement passes control to the next iteration of its closed iteration statement. The GOTO statement directly passes program control to the mark 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. The Return Statement terminates the execution of the method in it and returns the control to the calling method. It can also return an optional value. If the method is void type, you can omit the return statement. Throw statements are used to send signals that encounter abnormal conditions (Exceptions) during program execution. Generally, throw statements are used together with try-catch or try-finally statements. When an exception is thrown, the program looks for the catch statement that handles the exception. You can also use the throw statement to re-raise caught exceptions. ----------------------------------------------------------- Break example ---------- In this example, the condition statement contains a counter from 1 to 100, but the break statement terminates the loop after the count reaches 4. // Statements_break.cs using system; Class breaktest {static void main () {for (INT I = 1; I <= 100; I ++) {if (I = 5) {break;} console. writeline (I) ;}} outputs 1 2 3 4 -------------- continue example -------------- in this example, the counter is initially counted from 1 to 10, however, by using the continue statement with the expression (I <9), the statement between the end of the continue and the for loop body is skipped. // Statements_continue.cs using system; Class continuetest {static void main () {for (INT I = 1; I <= 10; I ++) {if (I <9) {continue;} console. writeline (I) ;}} outputs 9 10 ---------- goto Example 1 ---------- the following example demonstrates the use of goto in the switch statement. // Statements_goto_switch.cs using system; Class switchtest {static void main () {console. writeline ("coffee sizes: 1 = small 2 = medium 3 = large"); console. write ("Please enter your selection:"); string S = console. readline (); int n = int. parse (s); int cost = 0; Switch (n) {Case 1: cost + = 25; break; Case 2: cost + = 25; goto case 1; Case 3: cost + = 50; goto case 1; default: console. writeline ("invalid Selection. "); break;} If (cost! = 0) {console. writeline ("Please insert {0} cents. ", cost);} console. writeline ("Thank you for your business. ") ;}} input 2 sample output coffee sizes: 1 = small 2 = medium 3 = Large please enter your selection: 2 Please insert 50 cents. thank you for your business. ---------- goto Example 2 ---------- 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 {static void main () {int x = 200, y = 4; int COUNT = 0; string [,] array = new string [x, y]; // initialize the array: For (INT I = 0; I <X; I ++) for (Int J = 0; j <Y; j ++) array [I, j] = (++ count ). tostring (); // read input: console. write ("Enter the number to search for:"); // input a string: String mynumber = cons Ole. readline (); // search: For (INT I = 0; I <X; I ++) {for (Int J = 0; j <Y; j ++) {If (array [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. ") ;}} enter 44 sample output enter the number to search for: 44 The number 44 is found. E Nd of search. ---------- throw example ---------- This example shows how to use the throw statement to raise an exception. // Throw example using system; public class throwtest {static void main () {string S = NULL; If (S = NULL) {Throw new argumentnullexception ();} console. write ("the string S is null"); // not executed }}
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.