This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Sir , Hope u r doin? fine . I m here with a problem n very much hoped that u will solve my problem .here is the 1st programe in java regarding Switch is given below:
my question is that: In the first programe,the default was given in the beginning and in the 2nd programe default was given in the last.so how the compiler comes to know the position of the default in the programe .how the compiler works internally to recognise default.i hope that u will make me underestand .i will wait for your reply. Take care Kumar Abhay
[This message has been edited by Cindy Glass (edited October 02, 2001).]
The compiler processes the case staatements until it finds one that matches, finds the default, if not matches, or if there is no default statement, until the ending brace of the switch statement. I know the default is usually put at the end, but you atre correct, it can be put anywhere within the switch case labels, and the compiler will know to process it. AS for how the compiler does that internally, I don't know. I just know it works!
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
'default' mathces any value. so if you place the default case at the top, whatever the value of (b+1), the default will get executed. for eg. if (b+1)=0, it will execute default, it will never go to case 0 (because of the break at the end of the case default). whereas using the 2nd code, the default will be executed only if no other case mathces. so, if value of (b+1)=0, then case 0 will be executed. i hope this helps to explain how the default is recognised/executed by the compiler.