Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Switch case

Reply
Thread Tools

Switch case

 
 
broli
Guest
Posts: n/a
 
      01-04-2009
Hello, I tried to execute this example on switch and I got the output:

i <= 1

public class MySwitch
{
public static void main (String [] args)
{
int i = 0;

switch (i)
{
case 0:

case 1:
System.out.println ("i <= 1");
break;

default:
System.out.println ("i > 1");
}
}
}

Can some one explain to me why I get this output ? I remember from my
C lessons that if you don't put a break in any of the cases (like in
case 0), the program enters into an infinite loop.
 
Reply With Quote
 
 
 
 
Lars Enderin
Guest
Posts: n/a
 
      01-04-2009
broli wrote:
> Hello, I tried to execute this example on switch and I got the output:
>
> i <= 1
>
> public class MySwitch
> {
> public static void main (String [] args)
> {
> int i = 0;
>
> switch (i)
> {
> case 0:
>
> case 1:
> System.out.println ("i <= 1");
> break;
>
> default:
> System.out.println ("i > 1");
> }
> }
> }
>
> Can some one explain to me why I get this output ? I remember from my
> C lessons that if you don't put a break in any of the cases (like in
> case 0), the program enters into an infinite loop.


You remember wrong. Case 0 simply falls through to case 1.
 
Reply With Quote
 
 
 
 
Patel
Guest
Posts: n/a
 
      01-12-2009
@ broli

public class MySwitch
{
public static void main (String [] args)
{
int i = 0;


switch (i)
{
case 0:


case 1:
System.out.println ("i <= 1");
break;


default:
System.out.println ("i > 1");
}
}



}

even if u put some statements at case 0: those will be executed and
will continue the case 1: statements because u dont have "break"
statement at case 0:, thats the use of "break" here...


--
Patel
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-12-2009
On Mon, 12 Jan 2009 02:39:20 -0800 (PST), Patel
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>even if u put some statements at case 0: those will be executed and
>will continue the case 1: statements because u dont have "break"
>statement at case 0:, thats the use of "break" here...



see http://mindprod.com/jgloss/switch.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-13-2009
Patel wrote:
> @ broli
>
> public class MySwitch
> {
> public static void main (String [] args)
> {
> int i = 0;
>
>
> switch (i)
> {
> case 0:
>
>
> case 1:
> System.out.println ("i <= 1");
> break;
>
>
> default:
> System.out.println ("i > 1");
> }
> }
>
>
>
> }
>
> even if u put some statements at case 0: those will be executed and
> will continue the case 1: statements because u dont have "break"
> statement at case 0:, thats the use of "break" here...


While what you said is correct, the use of ridiculously wide indentation,
excessive blank lines and leetspeak dilutes your effectiveness. I suggest
that you limit indentation to two (OK, maybe as much as four) spaces per
level, limit line width to 80 characters (some suggest 72 or even narrower),
and not be so lazy with your spelling.

It's also good practice to put a 'break' statement at the end of the last
'switch' case (the 'default' in your example).

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
case insensitive find on case sensitive stl map benhoefer@gmail.com C++ 1 04-06-2007 08:42 PM
lower case to upper case Janice C Programming 17 12-14-2004 02:35 PM
how to case select with case-insensitive string ? Tee ASP .Net 3 06-23-2004 07:40 PM
Possible to turn on/off cookieless sessions dynamically on a case by case basis at run-time? Steve Franks ASP .Net 2 06-10-2004 02:04 PM
Scorsese Collection: Keep case vs Snap case Ray DVD Video 0 05-30-2004 04:04 AM



Advertisments