Switch case, while, do while, Enum

Source: Internet
Author: User
Tags case statement switch case
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace C _ editing basics {// enumeration defines the value range of the variable Enum gender {male, female, unkown}; // declares a type, it is an enumeration type (defining an enumeration), which has three values. Male, female, unkown class program {static void main (string [] ARGs) {// ============================================== ======================== switch case statement // the value determined by switch case enters the branch that meets the conditions for running, it is mainly used to infer discrete values. It can only infer this I, for example, I = 100, run this code, for example I = 20, run that code, and so on. For example, how about I> 100, how about I <20... and so on, it is very difficult to infer the switch case. Note that "break" must be available after the statement ends. // int I = 8; // you can give I a default value or not: for example, int I = convert. toint32 (console. readline (); Switch (I) {Case 2: // equivalent to if (I = 2) console. writeline ("China"); break; Case 4: console. writeline ("America"); break; Case 6: console. writeline ("here is Europe"); break; case 8: Case 10: Case 12: // equivalent to if (I = 8 | I = 10 | I = 12). This is the only case where break is not required, the break console must be used for other tasks. writeline ("Eldest Brother, you are from Mars"); break; default: // equivalent to the IF statement Else console. writeline ("I really don't know where you are from! "); Break;} console. readkey (); // ================================================ ================================== while statement int I = 0; int sum = 0; while (I <100) // assume that I <100 outputs the sum of I from 0 to 100 and {I ++; sum = sum + I ;} console. writeline (SUM); console. readkey (); While (true) // if the value in parentheses is true, the code in braces will continue to run endlessly. {Console. writeline ("I am running, an endless loop");} // when the user entered a wrong password, the system always prompts "Password error. Please enter" console again. writeline ("Enter Password"); While (true) {string Password = console. readline (); If (password! = "Andmin") {console. writeline ("Password error, please input again");} else {console. writeline ("correct password"); break;} console. readkey (); // enables the user to continuously enter "Numbers". When the user inputs "end", the maximum number entered by the user is displayed. Int max = 0; while (true) {console. writeline ("enter a number"); string number = console. readline (); If (number = "end") {console. writeline ("the maximum value you just entered is {0}", max); console. readkey (); return;} int numberint = convert. toint32 (number); If (numberint> MAX) {max = numberint ;}} // ================================================ ================================== do while statement int I = 0; do {I ++; console. writeline (I) ;}while (I <-10); // regardless of the condition, the console is run first. readkey (); // ================================================ ============= use gender A = gender for enumeration. female; // value }}}

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.