Switch case

B

broli

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

Lars Enderin

broli said:
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.
 
P

Patel

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

Roedy Green

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
 
L

Lew

Patel said:
@ 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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top