Java basics: 11th selection Structure

Source: Internet
Author: User

 

The most basic program structure is executed from top to bottom according to the code writing order. Compile the work into a code sequence based on the functions to be completed, and then execute the work in this order. For example, the following code outputs numbers 1 to 5 system. out. println (1); system. out. println (2); system. out. println (3); system. out. println (4); system. out. println (5); when running the program, first execute the first line, then the second line ...... Until all rows are executed. In addition to this sequence structure, there are also the selection structure and loop structure. This content introduces the selection structure. Next time we will introduce the loop structure. Select StructureDuring program execution, certain statements are selected based on specific conditions. For example, to calculate the maximum values of Two integer variables A and B, it may be A or B, at this time, you need to choose the value of A as the maximum value based on the relationship between A and B, or the value of B as the maximum value. The selection structure includes two: If and switch. 1. If statementIf statements are executed when certain conditions are met. Basic Structure: If (condition expression) {statement} the result of the condition expression must be a Boolean value. You can execute multiple statements, and each statement ends with a semicolon. If the value of the conditional expression is true, the statement in parentheses is executed. If the value of the conditional expression is false, the statement in parentheses is not executed. [Example] calculate the maximum values of A and B. // Iftest. javapublic class iftest {public static void main (string [] ARGs) {int A = 10; int B = 8; int max = A; if (a <B) {max = B;} system. out. println ("maximum value:" + max);} the running result is: Maximum Value: 10. If there is only one line of code to be executed in the brackets, you can leave no parentheses, in the above code, the code in the main method can be rewritten to: int A = 10; int B = 8; int max = A; if (a <B) max = B; system. out. println ("maximum value:" + max); if statements can also be used to select to execute a group of statements, either the first group or the second group. The basic structure is as follows: if (condition expression) {Statement 1} else {Statement 2} if the value of the condition expression is true, Statement 1 is executed; otherwise, Statement 2 is executed. Statements 1 and 2 are always executed. Statement 1 and Statement 2 can both be multiple rows. If there is only one row, the corresponding parentheses can be omitted. For example, if and else are used to obtain the maximum value. Use the following code to replace the code in the main method in the preceding example. Int A = 10; int B = 8; int Max; if (a <B) {max = B;} else {max = A;} system. out. println ("maximum value:" + max); the running result is the same as above. The statements after else can also use the if statement. In this case, the structure will appear: If (condition expression 1) {Statement 1} else if (condition expression 2) {Statement 2} [else {Statement 3}] [else {Statement 3}] indicates whether or not this part of content can exist or not, depending on the situation. The purpose of this format is: if the result of conditional expression 1 is true, execute Statement 1. If the result of conditional expression 1 is false, and the result of conditional expression 2 is true, execute Statement 2. Otherwise, execute Statement 3 (if there is a final else ). At this time, the function is not to execute one statement in two statements, but to execute one statement in multiple statements. At this time, a lot of layers can be nested. [Example] the values of integer variable A may be 1, 2, 3, and 4. If a is 1, the output value is "addition operation" and a is 2. Then, the subtraction operation is performed, if a is 3, multiplication is performed. If a is 4, division is performed. // Ifelsetest. javapublic class ifelsetest {public static void main (string [] ARGs) {int A = 3; if (a = 1) system. out. println ("addition operation"); else if (a = 2) system. out. println ("subtraction"); else if (a = 3) system. out. println ("multiplication"); else system. out. println ("division operation") ;}} the running result is: if the value of a is changed during the multiplication operation, the running result will change accordingly. 2. Switch statementThe switch statement is executed in multiple cases. As in the preceding example, the switch statement can be used to output different contents based on the value of integer variable A, that is, to execute different statements. The basic structure of the switch statement is as follows: Switch (expression) {Case value 1: Statement 1 case value 2: Statement 2... Case value n: Statement n default: Statement n + 1} if the expression value is "value 1", the statement starts from Statement 1, if the expression value is "value 2", it is executed from Statement 2. If the expression value is "value n", it is executed from statement n, if the expression value does not match any case, execute default. The matching order is from front to back. Here, "Statement 1", "Statement 2", "Statement N", and "Statement n + 1" are not one or more statements. Let's take a look at the following example: [Example] // switchtest. javapublic class switchtest {public static void main (string [] ARGs) {int A = 2; Switch (a) {Case 1: system. out. println ("addition operation"); Case 2: system. out. println ("subtraction"); Case 3: system. out. println ("multiplication"); Case 4: system. out. println ("division operation"); default: system. out. println ("the value of A is invalid") ;}} the running result is: Perform the subtraction operation and perform the multiplication operation. Perform the division operation. The value of A is invalid. From the running result, as expected, only "perform subtraction" should be output, that is, the first line of the running result. The reason for this is that in the switch structure, all codes that match successfully are executed, that is, the case expression only determines the entry of the program. If the type of variable A is defined as long, an error is reported during compilation. So pay attention to two points: (1) The expression result can be short type, byte type, int type, char type, and enumeration interface type. Long, float, and double cannot be used. (2) If case matching is successful, only the entry of the program is determined. To complete the above functions, you need to add a break after each case statement. The beak statement can jump out of the selection structure and no longer execute the subsequent code. The modified code is as follows: [Example] // switchtest2.javapublic class switchtest2 {public static void main (string [] ARGs) {char a = 2; Switch (a) {Case 1: system. out. println ("addition operation"); break; Case 2: system. out. println ("subtraction"); break; Case 3: system. out. println ("multiplication"); break; Case 4: system. out. println ("division operation"); break; default: system. out. println ("the value of A is invalid") ;}} the result of running is: This is the expected result of the row subtraction operation. The IF statement and switch statement both select the structure. If the selection is small, the IF statement should be used. If there are many options, the switch should be used. In addition, the IF statement is of the boolean type, while the switch statement is of the integer type (including char type, but not long type) and enumeration type. [Note] whether using the if statement or the switch statement, you must put the most common conditions at the beginning, because the average number of matching times is reduced, if the most common options are placed at the end, almost all matching operations are performed each time. Last time: Lecture 10 basic operations-OperatorsNext time: 12th circular structureCsdn blog: http://blog.csdn.net/javaeeteacher invite you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

 

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.