3. C # basic sorting (statement Overview ),

Source: Internet
Author: User
Tags switch case

3. C # basic sorting (statement Overview ),
Statement

Statements are classified into four categories:

* Branch statement: if, if... else, if... else, switch case
* Loop statement: for, while, do while, foreach
* Jump statement: break and continue
* Exception statement: try catch finally

Usage:

1. if usage

First: if (true) {statement} else {statement}

Type 2: if (true) {statement} else if {statement }... N else if {statements }... Else {statement}

Third: if (true) {statement}

* 2. switch case usage (not commonly used)

Int I = 3; switch (I) {case 0: case 1: Console. writeLine ("1"); break; // each case must contain a break, goto, or return case 2: Console. writeLine ("Case 2"); break; // The following statement will be warned-syntax error Console. writeLine ("Unreachable code"); case 7-4: // 7-4 is estimated to be 3, and an error warning is raised to the Console. writeLine ("Case 3"); break; default: // This statement is required; otherwise, the switch case Console is not executed. writeLine ("input error ");}

3. Usage of

For (initial value; Execution condition; status change) {cyclic body} endless loop for (;) {cyclic body}

4. Usage of while and do while

While (true) {statement} ---- true is the judgment condition. The initial condition is written outside the loop body, and the state change is written in the loop body.

Do {statement} while (true )----Note: This loop will first execute the content in do, regardless of whether the content in the while brackets is true.

5. foreach traversal (used for arrays, which will be explained in detail)

6. break and continue usage

Break: end the loop;

Continue: jump out of this loop and continue the next loop;

* 7. try {} catch {} And try {} catch {} finally {}

Try ...... Catch ......

While (true) {int j; try {string s = Console. readLine (); // If a decimal number is input, the error j = int is returned. parse (s);} catch (Exception yi) // Exception is a class. This sentence captures the error type and assigns it to the variable {Console. writeLine (yi. message); // Output Error continue; // continue while loop} Console. writeLine (j); // execute this break if it is correct; // skip the loop}

Try ...... Catch ...... Finally ......

Int e = 0; try {string s = Console. readLine (); e = int. parse (s);} catch (Exception yi1) // Exception is a class {Console. writeLine (yi1.Message);} finally {Console. writeLine ("Final Statement: {0}", e );}

 

 

The statement is not complete ............

 

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.