Solved

JDK6: about switch

Posted on 2010-11-27
2
754 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now