Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

break in the case of switch statement inside for loop

 
Vanitha Pai
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Any help for this code would be greatly appreciated!

I have a code in the jsp shown below:

for(int i= 0; i< length; i++) {
switch(i){

case 0:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;
case 1:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;
case 2:
for(int j = 0; j < length; j++) {
<td>
//Do something
</td>
}
break;


Please note the places where I have used breaks. The JSP prints a message unreachable code as soon as I add break. Otherwise it loops through the switch and for loop and creates 'n' number of td's. Any limitation of using the break in this scenario?Please help
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at what line exactly you are getting the unreachabe code.I think you are getting this problem in last break.just remove that and try.
 
Swastik Dey
Rancher
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid using scriplets in jsp, it's a very old practice. Look for JSTL, EL, Custom tags etc.
 
Vanitha Pai
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshwin Sukhdeve wrote:at what line exactly you are getting the unreachabe code.I think you are getting this problem in last break.just remove that and try.


Hi Eshwin Sukhdeve,

Thanks for the suggestion but I have tried that and even then it doesn't work:(
 
Vanitha Pai
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:Avoid using scriplets in jsp, it's a very old practice. Look for JSTL, EL, Custom tags etc.


Thanks for the suggestion but this code is in a maintainence(very old)application and we have to use the scriptlets. It doesn't support JSTL and custom tags.
 
Stefan Evans
Bartender
Posts: 1687
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If thats the case then please post the code with "code" tags so that it gets formatted correctly.

You seem to be missing the scriptlet markers (<% / %>)delimiting what is java code and what is template code.
and also you are missing two '}' characters at the end to close your switch and outer loop.

yes. There is a reason we have moved on from mixing scriptlets and template text.
because this stuff is so hard to read and debug!

 
Vanitha Pai
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:If thats the case then please post the code with "code" tags so that it gets formatted correctly.

You seem to be missing the scriptlet markers (<% / %>)delimiting what is java code and what is template code.
and also you are missing two '}' characters at the end to close your switch and outer loop.

yes. There is a reason we have moved on from mixing scriptlets and template text.
because this stuff is so hard to read and debug!





Thanks for pointing out that the two curly braces were missing. Now I have added the code within the code tags and also the scriplets for java code and temlate code outside the scriptlets.

Please let me know if I have missed something.


 
Stefan Evans
Bartender
Posts: 1687
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I copy and paste this code into a jsp and try running it it will work right?
No?

# You don't declare 'length' anywhere
# You're missing a <% on line 23

However once those issues are fixed, it works exactly as I would expect it to.
I still think it would be better with JSTL. The technology has been around more than 10 years. What sort of outdated system are you using that its not available?

If you must persist with scriptlet code then ensure:
- all your brackets are lined up
- all your open/close scriptlet tags are in the right place

 
Paul Clapham
Sheriff
Pie
Posts: 20945
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why you would write

when you could use equivalent code which is much simpler:

 
Vanitha Pai
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I don't see why you would write

when you could use equivalent code which is much simpler:



Hi Paul,

Could you please elaborate the solution? I am not sure I understand the solution what you have mentioned. Thanks for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic