Jump Statement of main flow control statements in C #

Source: Internet
Author: User

The jump statement is used to change the execution process of the program and transfer it to the specified place. C # contains 4 medium jump statements, as shown in: 1. The Break statement can use the Break statement to terminate the current loop or its condition statement. Then, control the code lines after the embedded statement passed to the loop or condition statement. The syntax of a Break statement is extremely simple. It has no parentheses or parameters. You only need to place the following statements where you want to jump out of the loop or Condition Statement: break; the following code is a simple example of a Break statement: [csharp] <span style = "font-family: KaiTi_GB2312; font-size: 18px; "> int I = 9; while (I <10) {if (I> = 0) {Console. writeLine ("{0}", I); I --;} else {break ;}} running result: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 </span> 2. if a Continue statement contains the Continue keyword, the program will be partially executed in some cases, and the other part will not be executed. In a While loop statement, when an embedded statement encounters a Continue command, the program jumps to the top test condition of the loop unconditionally, and enters the loop after the condition is met. In the Do loop statement, when the embedded statement encounters a Continue command, the program flow jumps to the test condition at the bottom of the loop unconditionally. After the conditions are met, the program enters the loop. At this time, the program segment after the Continue statement will not be executed. Example of a Continue statement: an odd number between 10 numbers 1-10 is output. [Csharp] <span style = "font-family: KaiTi_GB2312; font-size: 18px;"> int I = 1; while (I <= 10) {if (I % 2 = 0) {I ++; continue;} Console. write (I. toString () + ","); I ++;} the output result of this program is 3, 3, 5, 7, 9 </span> 3. the Goto statement jumps out of the loop and reaches the identified position. A small example of a Goto statement: Use a Goto statement for data search. Program code: <span style = "font-family: KaiTi_GB2312; font-size: 18px;"> using System; using System. collections. generic; using System. text; namespace gotoExample {class Program {static void Main (string [] args) {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 Indium Ut: Console. write ("Enter the number to search for:"); // Input a string: string myNumber = Console. 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. writeLin E ("End of search. "); Console. readLine () ;}}</span> running result: 4. the Return Statement of the Return Statement is function-level. If the Return method is used, the Return statement must be returned, that is, the code after the Return statement is stopped. An example of a Return statement is a simple example of a return jump statement. Program code: [csharp] <span style = "font-family: KaiTi_GB2312; font-size: 18px;"> using System; using System. collections. generic; using System. text; namespace returnExample {class Program {static void Main (string [] args) {Console. writeLine (Add (1, 2); return; Console. writeLine ("can't be reached");} static int Add (int a, int B) {return a + B ;}}</span> running result analysis: an error occurred while running the above Code. The error is described as "inaccessible code is detected" and is displayed on the Console. wri TeLine ("can't be reached"); this code prompts that the Return statement has blocked subsequent statements.

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.