Cannot Jump from Switch Statement to this Case Label

Most Android users might not encounter this problem. But, for those who want to make the best of their Android devices, this could be a real nightmare. “Cannot jump from switch statement to this case label” error is usually encountered by Android programmers who are using C or C++ to build their app.

For those who don’t know yet, with your Android device, you can do whatever you want. Since it’s open software, you can make adjustments and modifications, as well as install your created apps on your phone.

Especially with Android Studio, the official Integrated development system (IDE) for Android applications from Google, you can easily create high-quality apps. It has custom-tailored tools for Android developers, so you can easily code, edit, debug, test, and so on.

Although the official language of Android development is Java, it’s possible to develop C and C++ apps. With the Android Native Development Kit (NDK) you can use C++, but Google isn’t promoting this method — hence, the Android Studio app. Other programming languages Android developers can use are C#, BASIC, PhoneGap, and more. On the side note, you also need to learn XML for the UI-related stuff.

Why Use C++ for Android Apps?

Even though C++ isn’t recommended for Android app development, still, a lot of enthusiastic Android developers prefer this over Java. One of the most obvious reasons is that when using native code on Android, you can experiment on the complexity of the app you make. For seasoned developers, they prefer C++ over Java because of the following:

Well-known Android Language

Google has Android NDK before. And, while it’s not useful for a great number of Android apps, it’s useful for CPU-intensive apps and game engines.

Existing Game Source Code

There are tons of free content and open-source game engines out there built in C++. While not all game apps are made in C++, there is a handful of them. And, you can put some things together to come up with a great Android app.

Faster Code

Famous apps like Facebook, Office, Skype, and even the game Clash of Clans use C++ for cross-platform development. The advantage of C++ is that it doesn’t have a standard user interface. In some cases, Java could be much faster to code, but it’s the native code that dominates the areas of gaming, physics, signal processing, etc. While this is also possible with JN! in Java, it may take time. Hence, C++ coding is still faster.

Yes, Java is an excellent and versatile programming language, but for Android development, C++ has more interesting advantages. Aside from the three reasons above, there are still a few more. C++ occupies a smaller memory footprint. Since it’s a superset of C, you can use most C programs to compile your code or reuse some C software.

Last, Java source code is compiled to bytecode in .jar files. During runtime, the JVM will load the .jar file bytecode and compile it to machine code. For C++ on the other hand, you can easily and quickly compare it with different available software.

Understanding Switch Statement and Case Label

Before discussing the solution to this problem, you must know the characteristics of the switch statement by heart. It’s a selection statement that is often used as an alternative for the if-else statement. This includes one or more switch sections. And, each switch section usually has one or more case labels (case or default label). Check below for a sample code block so you can visualize what the problem is.

You must understand how the switch statement works. For the false statement, it’ll move forward, but for the true statement, it’ll stop and break the loop. With the switch-statement, you can inevitably use several curly braces if you have different cases. You must be careful of these braces. When you’ll initialize a variable in one case, then, you use it in the succeeding case, the error also occurs. You must use explicit {} blocks to prevent this. See the example below.

Solution

As you can see, this problem isn’t a big deal. All you got to do is to write your code properly and put the curly braces in the right place. Even if you have several lines of codes and have different case labels in your switch-statement, the ‘cannot jump from switch statement to this case label’ error is just a problem of the curly braces.

Use Curly Braces

For every case label, it’s safe if you will enclose the condition in each case with a pair of curly braces. This simply means you need to wrap each case label explicitly. Now, the problem would be, there will be too many curly braces and you don’t know the proper pairing anymore. There are C++ editors and compilers that let you know upfront if you have missed an open or close curly brace. Using these programs will allow save you more time debugging and editing the codes.

Initialize Variable in Every Case Label

Define and initialize variables in each case level. Although you have declared the variable in the first case, if you’ll use it in the second, it will return an error. Especially with the curly braces, it’ll limit the scope per case label. So, if you need to use variables, have it in each case.

The Final Say

If you’re an experienced C++ developer and you want to break (maximize the potential of) your Android mobile phone, you can make Android apps in C++ without a problem. Especially with Google’s release of the Android Studio and the support it has for native language, you can make Android apps even with zero knowledge in Java.

Unfortunately, the most prominent issues on C++ is the ‘cannot jump from switch statement to this case level’ error. It may seem technical, but an increasing number of expert programmers find it’s just an issue on the curly braces. That’s all!

The curly braces may seem to be small things, but they’re powerful enough to make or break your code. You must know its usage and its proper location in the code to build an Android app that can change your life forever.

Be the first to comment

Leave a Reply

Your email address will not be published.


*