Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Bizzare behavior of IF..THEN

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Myles - 31 Jan 2006 04:31 GMT
Having in times past encountered very freakish behavior of IF...THE
clauses,  I now wish to bring up some of my observations for  commen
and possible explanation.  Below are 4 trumped-up cases each showin
the evaluation, or lack of it, of the conditions set out in eac
routine. Cases A and B are straightforward as they conform to what w
might expect. Namely, there is indication of serial evaluation of al
the conditions- whether met or not.

By contrast, Cases C and D betray the norm, at least seemingly so, a
conditions which should be positively evaluated appear to be untouched
(see the commented annotations for full appreciation).

Case A                   Case B   
'all 4 conds evaluated    'all 4 conds evaluated       
Sub test2()        Sub test3()   
If 4 < 5 Then         If 4 < 5 Then  'evaluated   
MSGBOX [/B]\"4<5\"    *MSGBOX *\"4<5\"   
IF 4 > 3 THEN        END IF   
*MSGBOX *\"4>3\"    IF 8 > 10 THEN 'EVALUATED   
IF 4 = 4 THEN        MSGBOX \"8>10\"   
*MSGBOX *\"4=4\"    END IF   
ELSE                    IF 3 = 3 THEN 'EVALUATED   
MSGBOX \"OTHER\"        *MSGBOX* \"3=3\"   
END IF                    ELSE              'EVALUATED   
END IF                    MSGBOX \"OTHER\"   
END IF                    END IF   
END SUB                    END SUB   

CASE C                              CASE D
                       *  'NONE IS EVALUATED INCLUDING COND
Sub test1()                 Sub test4()
If 4 < 5 Then 'evaluated                 If 12 < 5 Then
MSGBOX* \"4<5\"              MSGBOX \"12<5\"
IF 8 > 10 THEN*'NOT EVALUATED* IF 8 > 10 THEN
MSGBOX \"8>10\"                   MSGBOX \"8>10\"
IF 3 = 3 THEN *'NOT EVALUATED*  IF 3 = 3 THEN *'NOT EVALUATE
MsgBox "3=3"                  MsgBox "3=3"
Else [B]'not evaluated*              Else
MsgBox "other"                   MsgBox "other"
End If                               End If
End If                               End If
End If                               End If
End Sub                               End Sub

Bolded MsgBox refers to positive evaluation as it springs to life.

Your comments are welcomed.

Myle
Leith Ross - 31 Jan 2006 04:59 GMT
Hello Myles,

What has part of the logic flow has you confused? What results were yo
trying  to achieve that you didn't?

Sincerely,
Leith Ros
Myles - 31 Jan 2006 05:18 GMT
Hi Ross,

Forget about Cases A and B. In both Cases C and D, we would expect th
line:  *If 3 = 3 Then * to be positively evaluated and consequently ge
the *MsgBox "3=3"* to show. This doesn't happen, for some reason, i
there is one.

Myle
Leith Ross - 31 Jan 2006 05:31 GMT
Hello Myles,

Let's deconstruct example "C"

Sub test3()
If 4 < 5 Then 'evaluated
MsgBox "4<5"
If 8 > 10 Then 'not evaluated
MsgBox "8>10"
If 3 = 3 Then 'not evaluated
MsgBox "3=3"
Else 'not evaluated
MsgBox "other"
End If
End If
End If
End Sub

First off, this is a group of 3 nested IF statements. The condition o
the first statement determines whether execution passes to the nex
statement.

Since 4 is less than 5 in the first statement, the message box display
the fact. Executions now passes to the second nested IF statement.

Since the condition 8 > 10 is false, execution passes to the End Su
statement because it is the next executable line of code.

The third IF "3=3" never executes because of the second IF being false

Sincerely,
Leith Ros
Tim Williams - 31 Jan 2006 05:42 GMT
How about reposting in a readable format?

Tim

> Having in times past encountered very freakish behavior of IF...THEN
> clauses,  I now wish to bring up some of my observations for  comment
[quoted text clipped - 45 lines]
>
> Myles
Arvi Laanemets - 31 Jan 2006 06:56 GMT
Hi

It's very confusing, maybe you try to explayn more clearly.

In general, possible syntax variations for IF clause are:

1. (only LogicalCondition1 is taken into account)
If LogicalCondition Then Response

2. (the response for 1st True condition is returned. When no condition is
filled, the response for Else is returned)
If LogicalCondition1 Then
   Response1
ElseIf LogicalCondition2 Then
   Response2
...
Else LogicalConditionN Then
   ResponseN
End If

3. (either Resonse1 or Response2 are returned)
Iif(LogicalCondition,Response1,Response2)

A common point for all variants is, that always only one response (or no
responses) is returned. I somehow get the impression, that you are trying to
get several responses, are you?

Signature

Arvi Laanemets
( My real mail address: arvi.laanemets<at>tarkon.ee )

> Having in times past encountered very freakish behavior of IF...THEN
> clauses,  I now wish to bring up some of my observations for  comment
[quoted text clipped - 45 lines]
>
> Myles
Myles - 31 Jan 2006 14:04 GMT
Hi Ross,

In the face of the execution behavior,  your explanation sound
plausible. I am however still struggling to come to terms with th
logic behind it.

Is it not  intuitively appealing that excecution should terminate onl
when a condition evaluates  to  TRUE? Put another way, conventiona
reasoning would demand that the code should keep on "searching" pas
FALSE evaluations until a TRUE evaluation is met, and then only exi
the sub. It is all a bit of convoluted logic to me
Ron Rosenfeld - 31 Jan 2006 15:04 GMT
>Hi Ross,
>
[quoted text clipped - 7 lines]
>FALSE evaluations until a TRUE evaluation is met, and then only exit
>the sub. It is all a bit of convoluted logic to me.

It seems logical to me; you construct a statement so that if the test is true,
do this; and if the test is false; do that.

If you want it to do further testing if a test is false, then you need to make
doing those tests a consequence of the false result.

--ron
Myles - 31 Jan 2006 15:24 GMT
Hi Ron,

you wrote inter alia:
If you want it to do further testing if a test is false, then you nee
to make doing those tests a consequence of the false result.

Since the code exits upon encountering a FALSE evaluation, could yo
please give an illustration  of how you can base your tests on -
consequence of the false result-

Thanks

Myle
Ron Rosenfeld - 31 Jan 2006 21:13 GMT
>Hi Ron,
>
[quoted text clipped - 9 lines]
>
>Myles

You need to use the proper syntax.  Using either Else or ElseIF for the nested
testing.  (See HELP for the IF...Then...Else Statement)

============================
Sub fooD()
If 12 < 5 Then
   MsgBox ("12<5")
Else
       If 8 > 10 Then
           MsgBox ("8>10")
       Else
               If 3 = 3 Then
                   MsgBox ("3=3")
               Else
                   MsgBox ("other")
               End If
       End If
End If

End Sub
=============================

This now displays in the message box "3=3"

Or, using the Elseif:

==================================
Sub fooD()

If 12 < 5 Then
   MsgBox ("12<5")
ElseIf 8 > 10 Then
   MsgBox ("8>10")
ElseIf 3 = 3 Then
   MsgBox ("3=3")
   Else
       MsgBox ("other")
End If

End Sub
=================================

--ron
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.