Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JDK6: about switch

Posted on 2010-11-27
2
Medium Priority
?
764 Views
Last Modified: 2013-12-29
hi all,

the following codes give the output

9 10 10 d 13

but the variable s has actually value 9, 10 and 11.

can anyone explain how exactly here works?

Codes:

public class Ebb {

    static int x = 7;
    public static void main(String[] args) {
        // TODO code application logic here
        String s = "";
        for (int y = 0; y <3; y++) {
            x++;
            switch(x) {
                case 8: s += "8";
                case 9: s += "9";
                case 10: { s += 10; break;}
                default: s += "d";
                case 13: s+= "13";
            }
        }
        System.out.println(s);
    }

    static {
        x++;
    }
}

thanks,

wantime
0
Comment
Question by:wantime
  • 2
2 Comments
 
LVL 4

Expert Comment

by:aaver
ID: 34221775
In a switch clause, every case is performed after the match until break in encountered.
0
 
LVL 4

Accepted Solution

by:
aaver earned 2000 total points
ID: 34221807
In more detail:
In the loop x has the values 9, 10, and 11.
x = 9 -> the match is at case: 9 -> s += "9" // s = "9"
since no break is encountered the case: 10 is also performed -> s += 10 // s = "9" + 10 = "910"
now a break is encountered and the loop continues with the next value

x = 10 -> the match is at case: 10 -> s += 10 // s = "910" + 10 = "91010"
a break is encountered and the loop continues with the next value

x = 11 -> there is no match -> the match is at default:-> s += "d" // s = "91010" + "d" = "91010d"
since no break is encountered the case: 13 is also performed -> s += "13" // s = "91010d" + "13" = "91010d13"
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question