?
Solved

JDK6: about switch

Posted on 2010-11-27
2
Medium Priority
?
761 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

801 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