Can we have label inside switch case java [duplicate] - java

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();
}
}

Related

Print inside Switch

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...

How to combine user input into a score system (JFrame)

I am trying to make a proper marking system for my multiple choice quiz, but when I answer the quiz, only the default in my switch statement will appear, How can I put in the input?
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
int mark = 0;
switch (mark) {
case 5:
JOptionPane.showMessageDialog(null, "D");
break;
case 6:
JOptionPane.showMessageDialog(null, "C");
break;
case 7:
JOptionPane.showMessageDialog(null, "B");
break;
case 8:
JOptionPane.showMessageDialog(null, "A-");
break;
case 9:
JOptionPane.showMessageDialog(null, "A");
break;
case 10:
JOptionPane.showMessageDialog(null, "A+");
break;
default:
JOptionPane.showMessageDialog(null, "F");
break;
}
Please check the value of mark. It is 0 hence default is showing.
Please check.
Please put break; statement in all case like.
case 6:
JOptionPane.showMessageDialog(null, "C");
break;
You must use a break; after each statement, otherwise the rest will be evaluated and the last will count.
Here's an example taken from here:
switch(expression) {
case value :
// Statements
break;
case value :
// Statements
break;
// You can have any number of case statements.
default : // Optional
// Statements
}

Modification of value in switch case

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");
}
}
}

How do I get a console menu to repeat once I have carried out one function so that I can carry out another without the program closing?

I have set up a console menu like so:
int userOption = printMenu(sc);
while(userOption != 6){
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
}
However when I run this, it only allows me to carry out the method I want to properly once and then rather than return to my menu and allow me to continue using other functions on top of the one I have just used, it just keeps repeating the original function I used.
Can anybody help me out and give me some advice?
From what is given here you seem to not get another userinput. To solve it you need to get another userinput after executing the loop.
int userOption = printMenu(sc);
while(userOption != 6) {
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
userOption = printMenu(sc);
};
As a little additon:
To save yourself the double input you could simply rewrite the loop to a do while loop:
int userOption = 0;
do {
userOption = printMenu(sc);
switch(userOption) {
case 1: //function 1
break;
case 2: //function 2
break;
case 3: //function 3
break;
case 4: //function 4
break;
case 5: //function 5
break;
case 6: //
break
default: //statement asking for valid option
}
} while(userOption != 6);
adding to your comment, you might not store the value into userOption again, but just ask for the input.

How to use switch in array comparison? [duplicate]

This question already has answers here:
Using switch statement with a range of value in each case?
(20 answers)
Closed 7 years ago.
I want to compare array length with some int values. I can do it with if else but how to do it with switch because switch is fast and I want to use that in my project
switch (array.length) {
case array.length <= 11: // how to do this
break;
default:
break;
}
With if else I can do this:
if (array.length <= 5) {
//my is code here
}
else if (array.length <= 15) {
//my is code here
}
else if (array.length <= 10) {
//my is code here
}
You can't. switch can only test exact values.
You could do:
switch(array.length) {
case 0: case 1: case 2: case 3:
case 4: case 5: case 6: case 7:
case 8: case 9: case 10: case 11:
// do stuff
break;
default:
break;
}
But why do you want to do this? What makes you think it's faster?
switch is not the same as if (...) { ... } else { ... }. You can only use == within a case. You would have to do something like this:
int length = array.length;
switch (length) {
case 0:
case 1:
case 2:
[...]
case 11:
// your code here
break;
//other cases here
}
Notice the missing break-statements, they are quite important.
I would recommend this tutorial for more details.
You can't do it(as per your example) using switch. Since the values of case are constant expression (case *value*).
Switch statements operate by exact matches rather than comparisons like if does. You can do what you want by introducing a new variable like this:
int value = (array.length <= 11 ? 0 : (array.length <= 20 ? 1 : 2));
switch (value) {
case 0: // 11 or under
break;
case 1: // 12 to 20
break;
case 2: // 21 or more
break;
}
I don't know if this buys you much over if/else statements, but if you find it cleaner to code, you can do it.
In your if/elseif, the if(array.length<=10) will never run because if(array.length<=15) is checked above it.
Doing this with an if/elseif structure would require less lines of code.
If you want to do this using a switch/case statement, this would work:
int length = array.length;
switch(length) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5: {
System.out.println("Case 0<=length<=5 triggered.");
break;
}
case 6:
case 7:
case 8:
case 9:
case 10: {
System.out.println("Case 6<=length<=10 triggered.");
break;
}
case 11:
case 12:
case 13:
case 14:
case 15: {
System.out.println("Case 10<=length<=15 triggered.");
break;
}
default: {
System.out.println("Default case triggered.");
}
}

Categories