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 ............