Process control statement (C #)

Source: Internet
Author: User
1. SELECT statement

 

1.1 If... Else statement

The IF statement is used to select a statement for execution based on the value of a Boolean expression. The basic format is as follows:

If (Boolean expression) {[Statement block]}

Sample Code:

Staticvoid main (string [] ARGs) {int I = 333; // declare an int type variable I if (I> 998) // call the if statement to determine if I is greater than 998 {console. writeline ("I greater than 998"); // If I greater than 998, the output "I greater than 998"} else // otherwise {console. writeline ("I not greater than 998"); // output "I not greater than 998"} console. readline ();}

Running result:

Of course, there are more than one conditional type in the program, and a nested if... Else statement, that is, add another if statement or if… to the program block in the IF or else statement... Else statement.

1.2 switch statement

A switch statement is a multi-branch selection statement that enables the program to select a branch for execution from multiple branches based on the value of the expression. Basic Format:

Switch ([expression]) {case [constant expression]: [Statement block] Break; case [constant expression]: [Statement block] Break ;... Case [constant expression]: [Statement block] default: [Statement block] Break ;}

Sample Code:

Staticvoid main (string [] ARGs) {string mystr = "download you in a lifetime"; Switch (mystr) {// if the value of mystr is "download you in a lifetime ", execute branch 1 case "download you for a lifetime": console. writeline ("download you in a lifetime"); break; // if the value of mystr is "Love in a lifetime", execute branch 2 case "Love in a lifetime": console. writeline ("love in life"); break; // if the value of mystr is "none of the above branch content, run the default statement default: console. writeline ("no character"); break;} console. readline ();}

Running result:

2. Iterative statements

 

2.1 While statement

The while statement is used to execute a statement zero or multiple times based on the condition value. When each while statement completes the execution of the Sino-German code, it will re-check whether the condition value is met, if yes, execute the same code again. Otherwise, the while statement is displayed.

Sample Code:

Staticvoid main (string [] ARGs) {int S = 0, num = 80; // declare two int type variables and initialize while (S <num) // call the while statement, when S is less than num, execute {s ++; // s Auto-increment 1 If (S> 40) // use the if statement to determine if S is greater than 40 {break; // if it is greater than 40, use the break statement to terminate the loop} If (S % 2 = 0) // if it is less than 40, call the if statement to determine whether s is an even number {continue; // if the result meets the condition again, go to the next loop} console. writeline (s);} console. readline ();}

Running result:

2.2 do... While statement

Do... The while statement is similar to the while statement. The difference is that its judgment condition is after the loop. Do... The while loop is executed before the conditional expression is calculated.

Sample Code:

Staticvoid main (string [] ARGs) {bool term = false; // declare a bool type variable term and initialize it to false int [] myarray = new int [5] {0, 1, 2, 3, 4}; // declare an int array and initialize do // call do... while statement {for (INT I = 0; I <myarray. length; I ++) // call the for statement to output all data in the array {console. writeline (myarray [I]); // output data in the array} while (TERM); // set do... while statement condition console. readline ();}

Running result:

2.3 For statement

The for statement is used to calculate an initialization sequence. When a condition is true, the nested statement is repeatedly executed and an iteration expression sequence is calculated. If it is false, the loop is terminated and the for loop is exited.

Sample Code:

Static void main (string [] ARGs) {int [] Myint = new int [5]; // declare an integer array with five elements: Myint [0] = 0; // Add the first element Myint [1] = 1 to the array; // Add the second element Myint [2] = 2 to the array; // Add the third element Myint [3] = 3 to the array; // Add the fourth element Myint [4] = 4 to the array; // Add the fifth element for (INT I = 0; I <Myint. length; I ++) // call the for loop statement {console. writeline ("the value of Myint [{0}] is: {1}", I, Myint [I]); // output result} console. readline ();}
                         

Running result:

2.4 foreach clause

The foreach statement is used to enumerate the elements of a set and execute an embedded statement for each element in the set.

Sample Code:

Staticvoid main (string [] ARGs) {arraylist alt = new arraylist (); // instantiate the arraylist class alt. add ("download you in a lifetime"); // use the add method to add data ALT to the object. add ("love in life"); Alt. add ("stay old, learn Old"); Alt. add ("Smile"); Alt. add ("Lily"); console. writeline ("your favorite network names are:"); // The output prompt foreach (string internetname in alt) // use the foreach statement to output data {console. writeline (internetname); // output all data in the arraylist object} console. readline ();}

Running result:

3. Jump statement

 

3.1 break statement

The break statement can only be used in switch, while, do... In a while, for, or foreach statement, the break statement is included in these statements. Otherwise, a compilation error occurs. When multiple switches, while, do... When the while, for, or foreach statements are nested with each other, the break statement can only be used with the innermost layer. To traverse multiple nested layers, you must use the GOTO statement.

The sample code is described above and will not be listed here.

3.2 continue statement

The continue statement can only be applied to while, do... A while, for, or foreach statement is used to ignore the code behind it in the loop statement block and start a new loop directly.


The sample code is also as follows.

3.3 GOTO statement

The GOTO statement is used to transfer the control to the statement marked by the tag. The GOTO statement can be applied to the case tag and default tag in the switch statement, and the tag declared by the tag statement.

Sample Code:

Staticvoid main (string [] ARGs) {console. writeline ("Enter the text to be searched:"); string inputstr = console. readline (); string [] mystr = new string [5]; mystr [0] = "download you all in one's life"; mystr [1] = "see each other "; mystr [2] = "love in life"; mystr [3] = "qingjian"; mystr [4] = "elegant and elegant"; for (INT I = 0; I <mystr. length; I ++) {If (mystr [I]. equals (inputstr) {goto found;} console. writeline ("the {0} You are looking for does not exist! ", Inputstr); goto finish; found: console. writeline (" {0} You are searching for exists! ", Inputstr); finish: console. writeline (" search complete! "); Console. Readline ();}

Running result:

3.4 Return Statement

The return statement is used to exit the class and is used to control the caller of the Return method. If the method has a return type, the return statement must return the value of this type. If the method does not have a return type, the return statement without an expression should be used.

Sample Code:

Static string mystr (string Str) // create a string type method {string outstr; // declare a string variable outstr = "your input data is:" + STR; // assign a return outstr value to the string variable; // use the return statement to return the string variable} staticvoid main (string [] ARGs) {console. writeline ("Enter the following content:"); // output the message string inputstr = console. readline (); // obtain the input data console. writeline (mystr (inputstr); // call the mystr method and display the result console. readline ();}

Running result:

How about the use of flow control statements in C?

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.