Question:
JAVA---my switch statement is hitting multiple cases off of one piece of data?
kid
2011-07-07 22:05:07 UTC
Here is my code, with the output beneath the line that says output:

for(int element = 0; element < 64; element++){
int current_data = data_to_process[element];

System.out.println("## value of current_data before switch: " + current_data);

switch(current_data){

case 1:
grid_cells[element] = new Regular_Block();
System.out.print("@@ in case 1");
System.out.println(" @@ value of current_data: " + current_data);
case 2:
grid_cells[element] = new Wood_Block();
System.out.print("@@ in case 2");
System.out.println(" @@ value of current_data: " + current_data);
case 3:
grid_cells[element] = new Metal_Block();
System.out.print("@@ in case 3");
System.out.println(" @@ value of current_data: " + current_data);
case 5:
grid_cells[element] = new Regular_Block();
System.out.print("@@ in case 5");
System.out.println(" @@ value of current_data: " + current_data);

default:
System.out.print("@@ in default");
System.out.println("@@ value of current_data: " + current_data);



***output***


## value of current_data before switch: 3
@@ in case 3 @@ value of current_data: 3
@@ in case 5 @@ value of current_data: 3
@@ in default@@ value of current_data: 3



Now, that input above? That is all the output before the line above switch statement gets new input, in which the code continues to do this wonky thing. The input shows that the switch statement is sending the SAME number into 3 cases: one that matches the number, one that DOESNT match the number, and the default. WHY IS THE SWITCH STATEMENT SENDING ONE NUMBER INTO 3 CASES??
Five answers:
zedda_piras25
2011-07-07 22:07:58 UTC
switch(var){

case x:

instructions

break;

}



you forgot to break
modulo_function
2011-07-07 22:13:02 UTC
Vomit is correct. Without the break; the code will just continue to 'drop ;through' each case.



I might seem silly but it's not. When you have several values of your switch variable that require the same processing you can do this:



switch(var){



case 1:

case 2:

case 7:

...etc



break;

case 4:

...ect

break;

default:

}



Note that the cases do not have to be in any order, The default is used to handle any case not coded. It's most frequently used to catch an error condition.
?
2016-10-15 10:55:56 UTC
modern-day compilers will elect the desirable properly ideal construct to interchange the construct you've written on your code if needed. for instance, i'm highly certain maximum Java compilers will emit an if...else bytecode structure it quite is an similar because the change assertion on your Java code. some compilers are even smart adequate to finish user-friendly refactoring and optimisation of boolean expressions, thoroughly replacing the kind of if...else statements even as holding the unique semantics and bypass of administration. each person with a suitable training in compiler theory (which incorporates the compiler dev communities at sunlight and Microsoft) will evaluate an if...else assertion to be a technique of change, rather than any incorrect way round, in view that even an user-friendly if (without else) is often modelled as a change assertion by technique of compilers. it truly is because the compiler can practice optimisations to modify constructs a lot less stressful than can practice them to if...else constructs. for instance, the following 2 gadgets of code will bring about similar bytecode with in simple terms trivial transformations that do not warrant pointing out: change (situation) { case authentic: // Do functional situation stuff spoil; default: // Do negative situation stuff spoil; } ...and... if (situation) { // Do functional situation stuff } else { // Do negative situation stuff } In maximum languages there are grammatical transformations between both constructs, which incorporates scope regulations scuffling with variables from being declared rapidly in a case block (maximum languages require that you declare a sparkling scope first), yet there aren't any semantic transformations. So, in answer, there isn't any genuine good thing about creating use of one over the different, except in words of clarity. The exception is once you start up lacking out spoil statements, and permitting administration to drop by from one case to the subsequent; in this example the compiler has to generate further bytecode to regulate which case blocks should be finished, ensuing in a larger bytecode image and slower execution - now to not indicate the messy, unreadable and unmaintainable source code ;-) if you're quite in contact to work out the precise distinction, you'll want to study the bytecode your certain compiler emits for equivalent change and if...else constructs.
anonymous
2011-07-07 22:15:37 UTC
It seems that the problem is with the + current data which is added in each switch case and also you din't add a break after each case.



Check this examples of switch case you will get an idea:



http://www.roseindia.net/java/beginners/SwitchExample.shtml

http://www.sap-img.com/java/java-switch-statement.htm



Hope that helped you!
Person
2011-07-07 22:07:16 UTC
You need



break;



At the end of each case block.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...