• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Switch statement

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at this question:
Q57.What will be the result of compiling and running the given program?
Select one correct answer.
1 class Q57
2 {
3 public static void main(String arg[])
4 {
5 int j=0;
6 switch(1)
7 {
8 case 2: j+=1;
9 case 4: j+=2;
10 default: j+=3;
11 case 0: j+=4;
12 }
13 System.out.println(j);
13 }
14 }
Answer = Program compiles correctly and print 7 when executed as we have not used break
Can someone please explain this to me, and also why is there a (1) in the switch statement and what does it represent?
Thank You
Kamil
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because no case match,the default part is executed,but this block have not break statement, and the case 4 part is also executed,then the answer is 7.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kamil Dada:
Take a look at this question:
Q57.What will be the result of compiling and running the given program?
Select one correct answer.
1 class Q57
2 {
3 public static void main(String arg[])
4 {
5 int j=0;
6 switch(1)
7 {
8 case 2: j+=1;
9 case 4: j+=2;
10 default: j+=3;
11 case 0: j+=4;
12 }
13 System.out.println(j);
13 }
14 }
Answer = Program compiles correctly and print 7 when executed as we have not used break
Can someone please explain this to me, and also why is there a (1) in the switch statement and what does it represent?
Thank You
Kamil



Hello Kamil,
Basically break doesnt belong to switch/case statement.
So break's presence is optional...
The loop will run until it hits the break or till the end of the
scope. Thats what happenin here. 1 doesnt have a match so it calls default and runs all the way to till the end of the scope
catch/default behaves the same if there is no default...
Regarding switch(1), just a constant is evaluated everytime.
Not a good practice, but its legal...
Hope that helps you

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil! Try running the code a print some messages. You will see that if no cases matches the swith then the default case is executed. It doesn't matter where you put the default statement. If no break is found , then the code will continue throw the switchblock until it reaches a breakstatement or switchstatement ends. A switch statement can take any variable that it compatible with an int. Boolean is therefor not permitted!
Hope this helps.
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot guys!
so basically the int j = 0 has nothing to do with the switch(1) statement, they just put it there to trick us? So what exactly is the 1 inside the switch statement?
Thank You.
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm so what does the 1 inside the switch statement refer to?
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 inside the switch refer to the switch-expression which hav constant value of 1 , nuting else
it could very well have been
int i =1 ;
switch (i)
{
....
.....
got it ?
i call this a hard-coded switch !
------------------
Gagan (/^_^\)
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

will it switch variable a or b?
and is a default: statement required?
and can we use contine somelabelnamehere; instead of break?
Thanks alot
Kamil.
[This message has been edited by Kamil Dada (edited August 14, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil,
You seem to be very confused regarding Java literals. Keep in mind that computers have registers and an accumulator. The statements:
int a = 1;
int b = 2;
will just set some registers in memory and have reference variables to those registers. The statement:
switch( 1 )
{
}
will place the value 1 into a memory location and start trying to match case blocks by performing subtraction with the value in another memory location called an accumulator. Nothing actually gets switched! The values in the registers holding a and b are not changed at all. The statement
switch( a )
{
}
will copy the contents from the memory location of reference a to another memory location and use it to start matching case blocks ...
Manfred.
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Hmm I am still confused, take a look at this:

So then here, meaning that switch would take the variable 1 (which is not possible) and match it to the given case statements and then print out/change whatever is needed once it has found a match
but shouldnt his give a compilation error since there is no variable 1?
that was my first logic..
this is my second:
so it is up to us if we want to pass a variable to the switch statement or something of our own? eg:

or even

Am I right?
Thank You,
Kamil.
[This message has been edited by Kamil Dada (edited August 14, 2001).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Kamil,
Let me try to explain,
the general format of a switch statement is
switch (expression)
expression here is anything that will evaluate to/will have a final value of an int. The final value of the expression will then be compared to the cases that follows the switch
Now, writing something like
int a = 1;
switch (a)
will make the variable a be evaluated, meaning the value of a will be fetched in memory and that value will be considered as the value of the expression.
If you write something like
switch (1)
the 1 there is a constant (it is an int literal) and the value of course of the expression when evaluated is the integer 1.
You can even write something like
int a=5;
switch (a * 2)
In this case, the value of the expression is 10.
Whatever is contained in the ( ) of the switch expression should have a final value of something that can be evaluated to an int.
The only reason why they give sample questions like that is to make it easy to evaluate the expression in the switch.
In practice, it is probably not being done.
Just remember that, you look at the expression, determine its value. It does not matter whether it is a constant or a variable. What matters is the final value of the expression.
I hope I did not add to your confusion.

 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Hmm thanks! I think ive got it now
Just to confirm:
if we have:

likewise in:

Thanks,
Kamil.
reply
    Bookmark Topic Watch Topic
  • New Topic