Switch case

Discussion in 'Java' started by broli, Jan 4, 2009.

  1. broli

    broli Guest

    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.
     
    broli, Jan 4, 2009
    #1
    1. Advertising

  2. broli

    Lars Enderin Guest

    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.
     
    Lars Enderin, Jan 4, 2009
    #2
    1. Advertising

  3. broli

    Patel Guest

    @ 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
     
    Patel, Jan 12, 2009
    #3
  4. broli

    Roedy Green Guest

    On Mon, 12 Jan 2009 02:39:20 -0800 (PST), Patel
    <> 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
     
    Roedy Green, Jan 12, 2009
    #4
  5. broli

    Lew Guest

    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
     
    Lew, Jan 13, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,328
    Kevin Spencer
    May 6, 2005
  2. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,910
    Dale King
    Jul 9, 2003
  3. vic

    switch case statement

    vic, Mar 3, 2004, in forum: Java
    Replies:
    9
    Views:
    5,986
  4. Myster Ious
    Replies:
    10
    Views:
    1,138
    Myster Ious
    Aug 1, 2003
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    123
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page