Java learning notes-4. Java branch loop statements

Source: Internet
Author: User

This part is also familiar, but you need to pay attention to the use of switch statements.

 
Public class chose {public static void main (string ARGs []) {int grade = 3; Switch (grade) {Case 3: system. Out. println ("excellent performance! "); Break; Case 2: system. Out. println (" pass! "); Break; Case 1: system. Out. println (" need to be repaired again! "); Break; Case 0: system. Out. println (" need to be repaired again! "); Break; default: system. Out. println (" the score is incorrect! "); Break ;}}}

Note the following two points:

1. Break statement

 Add the break statement after each case, which indicates that the entire switch branch is exited. If you do not add a break,ProgramIt starts from the first place that meets the case conditions, and will be executed in subsequent cases.

2. Set the default statement

For some data that does not meet the conditions, the default statement must be used to determine the data, otherwise there is no mechanism to process the abnormal data.

Switch is used as follows:

 public class chose {public static void main (string ARGs []) {int grade = 3; Switch (grade) {Case 3: system. out. println ("Excellent! "); Break; Case 2: system. Out. println (" pass! "); Break; Case 1: system. Out. println (" need to be repaired again! "); Break; Case 0: system. Out. println (" need to be repaired again! "); Break; default: system. Out. println (" the score is incorrect! "); Break ;}}

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.