public class Sample {
public static void main(String[] args) {
int i = 9;
switch (i) {
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
}
}
}
Output:
default
zero
Although i was initialized as 9, how is it possible that case 0 was picked? What is the reason?
You miss a break statement after the first default case so the excution just falls through to the second case.Also put the Default case last so that it is only excuted after all the other cases are checked.
public static void main(String[] args) {
int i = 9;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
break;
}
}
When I ran this, it printed:
default
zero
You have a couple things wrong.
(1) Put the default case at the end, not beginning. Otherwise you'll automatically go into the default case.
(2) Put a break statement after each case. If you don't, you code will continue from the case without the break and execute the code from the next case (and the next) until it reaches a break.
Try this
public class Sample {
public static void main(String[] args) {
int i = 9;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
}
}
}
Link on Java switch statements --http://www.tutorialspoint.com/java/switch_statement_in_java.htm.
You missed using break in your default case thus the program continues to execution and prints "zero".
You are doing two mistakes.
you should place the default case always at the end
you are missing important breaks
Solution:
public class Sample
{
public static void main(String[] args)
{
int i = 9;
switch (i)
{
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("default");
}
}
}
Related
This question already has answers here:
In a switch statement, why are all the cases being executed?
(8 answers)
Closed last year.
I am working on the game where the columns of the board are represented by the characters but i would like to assign them an index.
I have decided to use the switch statement in that case, however it does produce the wrong result.
With the current code I attach, it gives me 14 as an index, however since the string is 7h, and it takes h as a char, it should give an index of 7. What Could be an issue? Thanks in advance!
public class Check {
public int columnToInt(char c) {
int index=0;
switch(c) {
case 'a':
index=0;
case 'b':
index=1;
case 'c':
index=2;
case 'd':
index=3;
case 'e':
index=4;
case 'f':
index=5;
case 'g':
index=6;
case 'h':
index=7;
case 'i':
index=8;
case 'j':
index=9;
case 'k':
index=10;
case 'l':
index=11;
case 'm':
index=12;
case 'n':
index=13;
case 'o':
index=14;
}
return index;
}
public static void main(String[] args) {
String myStr = "7h";
char c =myStr.charAt(1);
System.out.println("the char at position 1 is "+c);
Check check = new Check();
int result = check.columnToInt(c);
System.out.println(result);
}
}
Java switch statements can be a bit annoying to use. You need to use break or all the cases after the expected one will be executed as well.
switch(c) {
case 'a':
index=0;
break;
Alternatively you can use a return.
switch(c) {
case 'a':
return 0;
You must add the break keyword for each case.
For example:
case 'a':
index=0;
break;
otherwise next assignments are applied.
Why is it that the following code:
class swi
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");continue;}
case 'b':{ System.out.println(a); continue;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
Gives the error:
continue outside of loop
Falling through is the standard behavior for a switch statement and so, consequently, using continue in a switch statement does not make sense. The continue statement is only used in for/while/do..while loops.
Based on my understanding of your intentions, you probably want to write:
System.out.println("default");
if ( (a == 'a') || (a == 'b') ){
System.out.println(a);
}
I would also suggest that you place the default condition at the very end.
EDIT:
It is not entirely true that continue statements cannot be used inside switch statements. A (ideally labeled) continue statement is entirely valid. For example:
public class Main {
public static void main(String[] args) {
loop:
for (int i=0; i<10; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 9:
continue loop;
}
System.out.println(i);
}
}
}
This will produce the following output:
0
2
4
6
8
The continue-Statement may be used in loops and not in switch. What you probably want is a break.
Because you have a continue outside of a loop. continue is for jumping to the next iteration of a loop, skipping the remainder of the current iteration. But you don't have any loop in that code. What you want for breaking out of a switch case block is the keyword break (see below).
There's also no need to put every case block within braces (unless you want locally-scoped variables within them).
So something a bit like this would be more standard:
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:
System.out.println("default");
break;
case 'b':
System.out.println(a);
break;
case 'a':
System.out.println(a);
break;
}
System.out.println("Switch Completed");
}
}
There's also a school of thought that says the default condition should always be at the end. This is not a requirement, just a fairly widely-used convention.
Shouldn't you use break instead of continue?
continue simply moves directly to the next iteration of the loop.
break is used to break out of loops and switches.
Use break; instead of continue;
Continue:
for(x = 0; x < 10; x++)
{
if(x == 3)
continue;
else
DoIterativeWork();
}
Switch:
switch(a)
{
default:{ System.out.println("default"); break;}
case 'b':{ System.out.println(a); break;}
case 'a':{ System.out.println(a);}
}
You are using continue where you should be using break
continue inside switch??!!! only break can be kept inside switch.!
ur code should be,
class swi22
{
public static void main(String[] args)
{
int a=98;
switch(a)
{
default:{ System.out.println("default");break;}
case 'b':{ System.out.println(a); break;}
case 'a':{ System.out.println(a);}
}
System.out.println("Switch Completed");
}
}
Desired outcome is printing "i is zero" then one, two, three, four..
It seems like my for loop is working properly, i is getting to 5 every time I execute but none of my cases are true, so nothing is printing. What am I doing wrong?
public class SwitchTest {
public static void main(String[] args) {
int i;
for ( i=0; i < 5; i++); {
switch (i) {
case 0:
System.out.println("i is zero");
case 1:
System.out.println("i is one");
case 2:
System.out.println("i is two");
case 3:
System.out.println("i is three");
case 4:
System.out.println("i is four");
}
}
}
}
You need to add break statement after every Sysout like.
switch (i) {
case 0:
System.out.println("i is zero");
break;
case 1:
System.out.println("i is one");
break;
case 2:
System.out.println("i is two");
break;
case 3:
System.out.println("i is three");
break;
case 4:
System.out.println("i is four");
break;
default:
//some statement here.
}
Nothing gets printed because there's a semicolon after your for statement, and java doesn't complain because it's still valid syntax. But fix it and you'll find that all of your cases print out on every iteration. This is because a switch statement will execute from the relevant case, all the way to the bottom, unless you end each case statement by a break:
switch (i) {
case 0:
System.out.println("i is zero");
break; //"break" means "exit the switch block here, don't go any further"
case 1:
System.out.println("i is one");
break;
case 2:
System.out.println("i is two");
break;
case 3:
System.out.println("i is three");
break;
case 4:
System.out.println("i is four");
break; //This one is optional
}
For good style, you should also include a default case, but that's a story for another day...
This question already has answers here:
Break label in switch
(3 answers)
Closed 6 years ago.
I have below pseudocode code with switch block with 4 cases. In 4th case I have if else conditions and when some condition satisfies, I am reducing list size by 1 and it has to come back to case 4 again and execute from the beginning of the 4th case. I tried to create a label in case 4: but it is giving the compilation error.
switch(choice) {
case 1: /* do operations */
break;
case 2: /* do operations */
break;
case 3: /* do operations */
break;
case 4:
mylabel:
if(condition1) {
}
else if(condition2) {
}
else {
break mylabel;
}
break;
default :
}
Above code gives the compilation error. But I want the program flow to be something like that. So I tried below code:
switch(choice) {
case 1: /* do operations */
break;
case 2: /* do operations */
break;
case 3: /* do operations */
break;
case 4:
if(condition1) {
}
else if(condition2) {
}
else {
break case 4;
}
break;
default :
}
With above code still, I am facing compilation issues. Is there any alternative for achieving the same. Here I need to go back to the beginning of the same case statement from where I will break. Hence it is different.
use label and while loop. it will work
switch (choice) {
case 1: /* do operations */
break;
case 2: /* do operations */
break;
case 3: /* do operations */
break;
case 4:
mylabel:{
while(true){
if(condition1) {
}else if(condition2) {
}else {
break mylabel;// breaks the while-loop
}
}
}
default:
break;
}
public void switchFunction(String choice){
switch(choice) {
case 1:
do1();
break;
case 2: /* do operations */
break;
case 3: /* do operations */
break;
case 4:
recursiveFunction();
break;
default :
}
}
public void recursiveFunction(){
if(condition1){
doSomething();
}
else if(condition2){
doSomethingElse();
}
else{
/* You can call it as much as you want! */
recursiveFunction();
}
}
I was doing my homework (Generate Roman Numerals from numerical input from 1-10), and in doing so, I made a switch statement. My question is how do I return to the top of a switch statement if none of the input is selected in a case? There was nothing about it in our textbook nor could I find anything in the java documentation. Is this just something that cannot happen?
Personally I prefer the while loop to do this:
just to explain the basic idea: you initialize a boolean that will be the criteria whether the loop has to be repeated, you start the while loop and put the boolean at false because in most cases you only want to run it once, you start the switch and for all cases where you want to repeat the loop you put the boolean again equal to true.
boolean again= true;
while (again){
again= false;
switch(number){
case 1:
break;
case 2:
break;
default: again=true;
break;
}
}
you can break from in case or can write return statement if it is method.
public int method(int i){
int j=0;
switch(i){
case 1: ... return j;
....
}
}
Here is how to do it but I highly recommend you to restructure your code to avoid doing it.
public class Main {
public static void main(String[] args) {
int i = 0;
loop: for (;;) {
switch (i) {
case 1: System.out.println(i);
break loop;
case 2: // more stuff
break loop;
default:
System.out.println(i);
i = 1;
break;
}
}
}
}
public class Main {
public static void main(String[] args) {
int i=3;
switch (i) {
default:
i=1;
case 1:
System.out.println(i);
break;
case 2:
break;
}
}
}