aspose file tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes behind the scenes  { Question from JWhiz } Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "behind the scenes  { Question from JWhiz }" Watch "behind the scenes  { Question from JWhiz }" New topic
Author

behind the scenes { Question from JWhiz }

ersin eser
Ranch Hand

Joined: Feb 22, 2001
Posts: 1072
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 ]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Jim Bertorelli
Ranch Hand

Joined: Nov 28, 2001
Posts: 136
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

Joined: Jun 03, 2000
Posts: 5040
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!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: behind the scenes { Question from JWhiz }
 
Similar Threads
JSP scriptlet
Nested code in If statement
JSP Output Question
Tomcat madness!
jsp:setProperty question