Many cases with one break

Contribute code & patches
Forum rules
No support questions here please

Many cases with one break

Postby karuga on Tue Mar 15, 2011 4:49 am

public class SwitchClass
{
public static void main(String[] kiarie)
{
int points =12;
switch(points)
{case 12:
System.out.println("A");
System.out.println("Excellent!");

case 11:
System.out.println("A-");
System.out.println("Very Good!");

case 10:
System.out.println("B+");
System.out.println("Good");

case 9:
System.out.println("B");
System.out.println("Good");

case 8:
System.out.println("B-");
System.out.println("Good");
break;
case 7:
System.out.println("C+");
System.out.println("Fair");
break;

case 6:
System.out.println("C");
System.out.println("Fair");
break;

case 5:
System.out.println("C-");
System.out.println("Fair");
break;

case 4:
System.out.println("D+");
System.out.println("poor");
break;
case 3:
System.out.println("D");
System.out.println("poor"); //its optional for default to have break
break;

case 2:
System.out.println("D-");
System.out.println("poor");
break;

case 1:
System.out.println("E");
System.out.println("very poor");
break;

default:
System.out.println("Bad entry");
System.out.println("please check the points and enter again");
break;
}
}
}



I wanted Cases 12,11,10,9 and 8 to have just the statements for case 8,hence only one break.However,if i switch 12,all statements up to first break are shown in output.What is causing this?
karuga
Level 3
Level 3
 
Posts: 120
Joined: Wed May 12, 2010 1:49 am

Linux Mint is funded by ads and donations.
 

Re: Many cases with one break

Postby bitmason on Tue Mar 15, 2011 6:24 pm

if each case block has more than one statement, you must enclose all statements for that case block in curly braces. when compiled, all of the case blocks are assumed to be part of the case block for "12".
"The surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
- Calvin & Hobbes
User avatar
bitmason
Level 2
Level 2
 
Posts: 70
Joined: Mon Feb 21, 2011 12:27 am
Location: Arizona


Return to Code & Patches

Who is online

Users browsing this forum: No registered users and 1 guest