Select the structure if statement and switch statement.

Source: Internet
Author: User

Select the structure if statement and switch statement.

------- Android training, java training, and hope to communicate with you! ----------

 

1. Select the structure if statement format and Its Usage 

A: if statement format:

If (comparison expression 1) {statement body 1;} else if (comparison expression 2) {statement body 2;} else if (comparison expression 3) {statement body 3 ;}... else {statement body n + 1;} B: Execution Process: first, calculate the comparison expression 1 to see whether its return value is true or false. If it is true, execute statement body 1, if statement ends. If it is false, calculate the comparison expression 2 to see whether the returned value is true or false. if it is true, execute statement body 2 and if statement ends. If it is false, calculate the comparison expression 3 to see whether the returned value is true or false. If both are false, execute the statement body n + 1. C: Note: The last else can be omitted, but it is recommended that you do not omit it. The error value out of the range is prompted as eg:
1 import java. util. declare; 2 class Demo_If {3 public static void main (String [] args) {4 then SC = new then (System. in); // keyboard input, 5 while (true) {6 System. out. println ("Enter your score"); // you are prompted to enter 7 int a = SC. nextInt (); // use the int type to receive 8 if (a> 100 | a <0) {// infinite loop for Testing 9 System. out. println ("Incorrect score"); 10} else if (a >=90 & a <= 100) {11 System. out. println ("A, etc."); 12} else if (a >=80 & a <90) {13 System. out. println ("B, etc."); 14} else if (a >=70 & a <80) {15 System. out. println ("C, etc."); 16} else if (a >=60 & a <70) {17 System. out. println ("D, etc."); 18} else if (a <60) {19 System. out. println ("E, etc."); 20} else {21 System. out. println ("Incorrect score"); 22} 23} 24} 25}

 

2. Select the format and usage of the structure switch statementA: switch format: switch (expression) {case value 1: Statement body 1; break; case value 2: Statement body 2; break ;... Default: Statement body n + 1; break;} B: Format explanation of the switch statement (basic data type, can be upgraded to int as long as it can be referenced in the Data Type enumeration (JDK1.5) and String (JDK1.7) C: the execution process first calculates the value of the expression and then matches the value following the case. If yes, the corresponding statement is executed. Otherwise, the default control statement eg is executed.
1 import java. util. *; 2 class Dome_If3 {3 public static void main (String [] args) {4 // System. out. println ("Hello World! "); 5 then SC = new then (System. in); // enter 6 while (true) on the keyboard {// it is convenient to Test 7 System. out. println ("Enter the number of weeks to be converted"); // The keyboard entry prompt is 8 in week = SC. nextInt (); 9 switch (week) {10 case 1: 11 System. out. println ("1"); 12 break; 13 case System. out. println ("2"); 15 break; 16 case System. out. println ("3"); 18 break; 19 case 4: 20 System. out. println ("4"); 21 break; 22 case 5: 23 System. out. println ("5"); 24 break; 25 case 6: 26 System. out. println ("6"); 27 break; 28 case 7: 29 System. out. println ("Sunday"); 30 break; 31 default: 32 System. out. println ("the number you entered is incorrect. Please enter it again"); 33} 34} 35} 36}

 

3: Summarize the use cases of switch and if statements.When the switch is used to determine the fixed value, if is used to determine the range or range. if is used to determine the range or range, the switch can be used to do this. if it is used, the SLR won't work.

------- Windows Phone 8 mobile Phone development,. Net training, and we look forward to communicating with you! -------

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.