• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

behind the scenes { Question from JWhiz }

 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question says is following going to compile?
<%! int i ; %>
<% if (i==0)%>
<% i++; %>
<% else %>
<%= "i==1" %>
I said yes, thinking one line statements does NOT need braces
They say No won't compile because missing braces. Well I don't buy that I tried it and did not compiled saying else without if . No ! Why ? What do you think ?
Ok then go to work directory and see the code when no braces
out.write("\r\n");

if (i==0)
out.write("\r\n");
i++;
out.write("\r\n");
else
out.write("\r\n");
out.print( "i==1" );

Heh he not the braces the line brakes messes you up !! Thus you need braces, but where ?
How about this :
<%! int i ; %>
<% if (i==0)%>
<% {i++;} %>
<% else { %>
<%= "i==1" }%>
What do you think ?
No ! this is what you end up having in _jspService
out.write("\r\n");
if (i==0)
out.write("\r\n");
{i++;}
out.write("\r\n");
else {
out.write("\r\n");
out.print( "i==1" });
Heh he not what you expected !
Torture continues
<%! int i ; %>
<% if (i==0) {%>
<% i++;} %>
<% else { %>
<%= "i==1" %>
<% }%>
What do you think ?
No Again !
out.write("\r\n");
if (i==0){
out.write("\r\n");
i++;}
out.write("\r\n");
else {
out.write("\r\n");
out.print( "i==1" });
Did you see that out.write that is the one messes your if structure.
Now I still say that the explanation of the answer was not proper missing thing is not the { and } the placement of them is important.
And I still insist that you don't need the braces
Here you go !
<%! int i ; %>
<%if(i==0)%><% i++; %><% else %><%= "i==1" %>
What do you think ?
Of course it works Here what you get after compilation:

out.write("\r\n");
if(i==0)
i++;
else
out.print( "i==1" );
Of course in all cases i is being declared as instance variable: int i ;
Watch those damn line breaks \r\n
[ February 01, 2002: Message edited by: ersin eser ]
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one ersin. Really good one.
Thanks.
Actually, without realising this
fact
I always used code like this
in my JSPs:
<%! int i ; %>
<% if (i==0) {%>
<% i++;%>
<% }else { %>
<%= "i==1" %>
<% }%>
You see the closing brace of the if stmt goes
just before the else keyword. And the else keyword is immediately followed by the opening brace.
regds.
- satya
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key to solving such questions is to visualize the given code in the servlet form. This will immediately tell you whether it'll compile or not.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the servlet form
yea....acc to what app server. Don't you think this is app server dependent. Well I now know how Tomcat does it... :roll:
- satya....can't stand the pressure of learning more!!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic