Process control statement (example)

Source: Internet
Author: User

1. switch statement: determines the season of the month entered by the user. namespace switch statement {class Program {static void Main (string [] args) {// determines the season Console for the month you entered. writeLine ("enter a month! "); Int MyMouth = int. parse (Console. readLine (); // variable MyMouth is used to obtain user input data string MySeason; switch (MyMouth) {case 12: case 1: case 2: MySeason = "this month is winter! Learn to keep warm! "; Break; // jump out of the switch statement case 3: case 4: case 5: MySeason =" this month is spring! Xue min remembers to wear glasses to avoid strong winds! "; Break; case 6: case 7: case 8: MySeason =" this month is summer! Xue min remembers to eat ice cream and wear hot pants! "; Break; case 9: case 10: case 11: MySeason =" this month is autumn! This is a good harvest season. I often call home greetings! "; Break; default: MySeason =" month input error! TIPS: only 1 ~ October December! "; Break;} Console. writeLine (MySeason); Console. readLine () ;}} running result: 2. while statement: declare two int-type variables: s and num. The initial values are 0 and 100, respectively. loop output through the while statement. When s is greater than 50, use the break statement to terminate the loop. When s is an even number, use continue to start the next loop. Namespace while statement {class Program {static void Main (string [] args) {// when s> 50, use the break statement to terminate the loop. When s is an even number, use continue to start the next loop int s = 0; int num = 100; while (s <num) {s ++; if (s> 50) {break ;} if (s % 2 = 0) {continue;} Console. write (s + "");} Console. writeLine ("\ n min, the above is the output of all odd numbers from 0 to 50! Please check it out! "); Console. readLine () ;}} running result: 4. for statement: declare an int-type array, add five values to the array, and use the for loop statement to traverse the array, and output the values in the array. namespace for statement {class Program {static void Main (string [] args) {string [] xuemin; xuemin = new string [5]; // declare a string array with five elements. xuemin [0] = "Hello! "; // Add the element xuemin [1] =" My name is Han xuemin "; xuemin [2] =" I like computer learning! "; Xuemin [3] =" thank you for your support !!! "; Xuemin [4] =" I will work hard and devote of my enthusiasm! "; For (int I = 0; I <xuemin. length; I ++) // use the for statement to output each element in the array {Console. writeLine ("xuemin [{0}] value: {1}", I, xuemin [I]);} Console. readLine () ;}}5, foreach statement: traverse the array namespace foreach statement {class Program {static void Main (string [] args) {int count; Console. writeLine ("Enter the number of friends of student sensitivity"); count = int. parse (Console. readLine (); string [] names = new string [count]; // declares the array names. The number of elements in the array is the number of friends in the input field. count (Int I = 0; I <names. length; I ++) {Console. writeLine ("enter the name of {0} friend of student sensitivity:", I + 1); names [I] = Console. readLine ();} Console. writeLine ("friends who have output learning sensitivity are as follows:"); foreach (string name in names) // The foreach statement traverses the Array {Console. writeLine ("{0}", name);} Console. readKey () ;}}6, break statement: for the application example of the switch statement and the while statement, see the application of 1 and 2 in the for statement above: namespace break statement {class Program {static void Main (string [] args) {for (int I = 0; I <3; I ++) {Console. write ("\ n {0} cycles:", I); // The output shows the number of cycles for (int j = 0; j <200; j ++) {if (j = 12) // if the value of j is 12 break; // terminate the cyclic Console. write (j + ""); // output j} Console. readLine () ;}}7. for the use of the while statement, see Example 2. the usage of for statements is similar. no more examples. 8. goto statement: the program jumps to the specified statement through the goto statement. Namespace goto statement {class Program {static void Main (string [] args) {Console. writeLine ("Enter the query text:"); string inputstr = Console. readLine (); // assign the input text to the inputstr variable string [] Mystr = new string [5]; // declare a string array, the array name is Mystr [0] = "Thank you! "; // Assign Mystr [1] =" to each array element. I'm not happy today. "; Mystr [2] =" very serious consequences! "; Mystr [3] =" I'm so hungry! "; Mystr [4] =" when to eat? "; For (int I = 0; I <Mystr. length; I ++) {if (Mystr [I]. equals (inputstr) // determines whether the input text exists in the array. If yes, the query jumps to the Found statement {goto Found;} Console. writeLine ("the" {0} "you are looking for does not exist", inputstr); // if it does not exist, output the prompt statement and jump to the Finish statement goto Finish; Found: Console. writeLine ("the" {0} "you are looking for exists! ", Inputstr); Finish: Console. writeLine ("\ n search completed"); Console. readLine () ;}} 9, return Statement namespace return Statement {class Program {static string MyStr (string str) {string OutStr; // declare a string variable OutStr = "\ n Han Zong (Han xuemin) Your input data is" + str; // assign a value to return OutStr for the variable OutStr; // return the string variable OutStr} static void Main (string [] args) {Console. writeLine ("Han Zong, please enter the meeting content:"); // output the prompt message string inputstr = Console. readLine (); // obtain the input data and assign it to the variable inputstr Console. writeLine (MyStr (inputstr); // call the MyStr method and display the result to the Console. readLine ();}}}

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.