Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

77%
+5 −0
Q&A Why is the switch statement not executing the correct case blocks?

The problem is the fallthrough behaviour of case statements. Basically, once a case's condition is met, all the others after that are also executed. Example: int x = 2; switch (x) { case 1: ...

posted 3y ago by hkotsubo‭  ·  edited 3y ago by hkotsubo‭

Answer
#2: Post edited by user avatar hkotsubo‭ · 2021-09-24T20:37:30Z (about 3 years ago)
  • The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example:
  • ```java
  • int x = 2;
  • switch (x) {
  • case 1:
  • System.out.println("one");
  • case 2:
  • System.out.println("two");
  • case 3:
  • System.out.println("three");
  • default:
  • System.out.println("default");
  • }
  • ```
  • The output of this code is:
  • ```none
  • two
  • three
  • default
  • ```
  • Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement if each `case`:
  • ```java
  • int x = 2;
  • switch (x) {
  • case 1:
  • System.out.println("one");
  • break;
  • case 2:
  • System.out.println("two");
  • break;
  • case 3:
  • System.out.println("three");
  • break;
  • default:
  • System.out.println("default");
  • }
  • ```
  • Now it prints only `two`.
  • ---
  • That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough.
  • But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this:
  • ```java
  • String order = null;
  • switch (i) {
  • case 0:
  • order = CallLog.Calls.DATE + " DESC ";
  • break;
  • case 1:
  • order = CallLog.Calls.DATE + " ASC ";
  • break;
  • case 2:
  • order = CallLog.Calls.CACHED_NAME + " ASC ";
  • break;
  • default:
  • order = CallLog.Calls.DATE + " DESC ";
  • }
  • if (order != null) {
  • new Handler().postDelayed(() -> {
  • contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer");
  • recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
  • recyclerViewCallLog.setAdapter(contactViewAdapter);
  • recyclerViewCallLog.invalidate();
  • recyclerViewCallLog.getAdapter().notifyDataSetChanged();
  • }, 1000);
  • }
  • ```
  • I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.
  • The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example:
  • ```java
  • int x = 2;
  • switch (x) {
  • case 1:
  • System.out.println("one");
  • case 2:
  • System.out.println("two");
  • case 3:
  • System.out.println("three");
  • default:
  • System.out.println("default");
  • }
  • ```
  • The output of this code is:
  • ```none
  • two
  • three
  • default
  • ```
  • Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement in each `case`:
  • ```java
  • int x = 2;
  • switch (x) {
  • case 1:
  • System.out.println("one");
  • break;
  • case 2:
  • System.out.println("two");
  • break;
  • case 3:
  • System.out.println("three");
  • break;
  • default:
  • System.out.println("default");
  • }
  • ```
  • Now it prints only `two`.
  • ---
  • That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough.
  • But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this:
  • ```java
  • String order = null;
  • switch (i) {
  • case 0:
  • order = CallLog.Calls.DATE + " DESC ";
  • break;
  • case 1:
  • order = CallLog.Calls.DATE + " ASC ";
  • break;
  • case 2:
  • order = CallLog.Calls.CACHED_NAME + " ASC ";
  • break;
  • default:
  • order = CallLog.Calls.DATE + " DESC ";
  • }
  • if (order != null) {
  • new Handler().postDelayed(() -> {
  • contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer");
  • recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
  • recyclerViewCallLog.setAdapter(contactViewAdapter);
  • recyclerViewCallLog.invalidate();
  • recyclerViewCallLog.getAdapter().notifyDataSetChanged();
  • }, 1000);
  • }
  • ```
  • I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.
#1: Initial revision by user avatar hkotsubo‭ · 2021-09-24T12:05:17Z (about 3 years ago)
The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example:

```java
int x = 2;
switch (x) {
    case 1:
        System.out.println("one");
    case 2:
        System.out.println("two");
    case 3:
        System.out.println("three");
    default:
        System.out.println("default");
}
```

The output of this code is:

```none
two
three
default
```

Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement if each `case`:

```java
int x = 2;
switch (x) {
    case 1:
        System.out.println("one");
        break;
    case 2:
        System.out.println("two");
        break;
    case 3:
        System.out.println("three");
        break;
    default:
        System.out.println("default");
}
```

Now it prints only `two`.

---
That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough.

But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this:

```java
String order = null;
switch (i) {
    case 0:
        order = CallLog.Calls.DATE + " DESC ";
        break;
    case 1:
        order = CallLog.Calls.DATE + " ASC ";
        break;
    case 2:
        order = CallLog.Calls.CACHED_NAME + " ASC ";
        break;
    default:
        order = CallLog.Calls.DATE + " DESC ";
}
if (order != null) {
    new Handler().postDelayed(() -> {
        contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer");
        recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
        recyclerViewCallLog.setAdapter(contactViewAdapter);
        recyclerViewCallLog.invalidate();
        recyclerViewCallLog.getAdapter().notifyDataSetChanged();
    }, 1000);
}
```

I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.