The location of the defaul condition in the switch statement in Java

Source: Internet
Author: User
Tags case statement

In the switch statement in Java, each case branch is a single entry, and if none of the conditions are met, it will go to the default branch. So will the location of the default branch affect the execution process?

Package com.app.statement;import java.util.scanner;/** * created by charles  on 2015/7/12. */public class SwitchTest {    public  Static void main (String[] args) {         System.out.print ("Enter a digital number:");         Scanner scanner = new scanner (system.in);         Int number = scanner.nextint ();         switch (number) {             default:                 SYSTEM.OUT.PRINTLN ("Default");                 break;             case 1:                 system.out.println ("1");                 break;            case  2:                 System.out.println ("2");             case 3:                  System.out.println ("3");             case 4:                  System.out.println ("4");         }    }}

Test conditions: 5

Execution Result: Default

Description if the case statement does not match, the position of the Defualt statement does not affect the process.


Test conditions: 2

Execution Results: 2 3 4

After the entry of the case statement, the flow of the statement executes sequentially down, not to the default statement. So the default statement should be written in the last place, or it might not be executed. Test jdk:1.7

The location of the defaul condition in the switch statement in Java

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.