Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's The Correct Answer? 2

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
Consider the following code snippet:

<%
If False Then
%>
<!-- #INCLUDE FILE=&quot;FunctionOne.inc&quot;-->
<%
Else
%>
<!-- #INCLUDE FILE=&quot;FunctionTwo.inc&quot;-->
<%
End If
%>

What would the above code load?

A. Only the FunctionTwo.inc file into the ASP page.
B. Both files, since Server Side Includes are processed
before ASP interpreting.
C. Only the FunctionOne.inc file into the ASP page.
D. Neither file, since Server Side Includes are processed
before ASP interpreting.

Out of the 4 given options, 'A' is the correct answer i.e. only FunctionTwo.inc will be loaded. Can somebody explain me why only FunctionTwo.inc will be loaded i.e. why option A is the correct answer among the 4 options given?

Thanks,

Arpan
 
Hmmm
I have ASP conditional includes, all the includes are loaded but only the include that meets the conditions is processed Saturday 12.00
im6.gif
im2.gif
im2.gif
20.00
im5.gif
im4.gif
im7.gif
3.00am
im8.gif
Sunday [img http
 
An IF condition is defaulted to FALSE unless a condition is found to be true.. Almost like the law (Innocent until found guilty).

So by asking if the IF condition is FALSE, this condition is met and ONLY loads and processes the include file that it is instructed to.

There is no other simple way to explain this process (that I can think of)

Cheers,

G.
 
it sounds like you are not having a problem with includes, but a lack of understanding of IF statements.

Dim A As String
If False Then
A = &quot;IfResult&quot;
Else
A = &quot;ElseResult&quot;
End If
MsgBox(A.ToString)

What would show up will always be &quot;ElseResult&quot;. What you are doing is short circuiting the IF statement and not letting it figure out if some condition is TRUE or FALSE, you are just telling it that this IF statement is FALSE.

This is because the IF statement is looking for a TRUE statement and the ELSE is looking for a FALSE statement.

Another way to look at it is, the IF statement is looking for a &quot;yes you are correct&quot; statement and the ELSE is looking for a &quot;no you are not correct&quot; statement.

So, here are a few samples:

-----------

Dim bValue As Boolean = FALSE
If NOT bValue Then
MsgBox(&quot;I will be executed.&quot;)
'It is TRUE(correct) that bValue is &quot;NOT TRUE&quot;.
Else
MsgBox(&quot;I will NOT be executed.&quot;)
End If

-----------

Dim bValue As Boolean = TRUE
If NOT bValue Then
MsgBox(&quot;I will NOT be executed.&quot;)
Else
MsgBox(&quot;I will be executed.&quot;)
'It is FALSE(incorrect) that bValue is &quot;NOT TRUE&quot;.
End If

-----------

I hope this helps.

Kris
- In today's fast-moving tech environment, it is a requirement that we forget more than we learn.
 
Hi Kris,

I have got to confess that you have indeed given a very very good explanation. Thanks for the same.

Regards,

Arpan
 
Both files are always loaded, no mather what branch of the if executes. Both files will give you sintax (if there is any in the file) errors and both files must exist for the page to run.
 
Hi Kris,

Another question I would like to ask you - consider the following 3 code snippets:

<%
iValue=5.22
If(iValue) Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>

----------------------

<%
strValue=&quot;5.22&quot;
If(strValue) Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>

-----------------------

<%
strValue=&quot;abcd&quot;
If(strValue) Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>

-------------------------

Now what will be the output of the above 3 code snippets & why?

Thanks once again,

Regards,

Arpan
 
The first two should both return true and print the hello1 statement.
Basically the first will be evaluated, it will be found to not be 0 (false) and will therefore be true by default.
The second will evaluate as a number as well, and follow the same path as the first.
The third,on the other hand, cannot be converted to anything but a string and therefore should throw an error.

Basically the statement is changing the datatype to fit it's needs. If you were to place a numeric 0, a &quot;0&quot;, or a &quot;false&quot; string in the if statement it should evaluate to false.

The if statement could be more accurately called an If Not If False statement, because if the expression is valid, it looks for equivalence to false, having only a single state, if it is anything but false it will be true (as long as it's a valid expression or can be easily converted to a valid expression)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Tarwn may be right or wrong, I have no idea.

Basically, that looks like bad programming. It may work, but it is hard to read and many people would not know how to read it.

In an IF statement you really should be comparing something to come up with a TRUE or FALSE answer that could be checked by hand.

And in general you should convert values your self and not let the language do (too many) default conversions. A lot of people do not fully understand how the language will convert things and this will cause trouble down the road.


<%
iValue=5.22
If iValue > 0 Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>

<%
strValue=&quot;5.22&quot;
If CDec(strValue) > 0 Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>

<%
strValue=&quot;abcd&quot;
If strValue = &quot;abcd&quot; Then
Response.Write(&quot;Hello1&quot;)
Else
Response.Write(&quot;Hello2&quot;)
End If
%>


These are all TRUE so Hello1 will write in all cases.

Kris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top