![]() |
Bizzare behavior of IF..THEN
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 *\"43\" IF 8 10 THEN 'EVALUATED IF 4 = 4 THEN MSGBOX \"810\" *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 \"810\" MSGBOX \"810\" 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 -- Myle ----------------------------------------------------------------------- Myles's Profile: http://www.excelforum.com/member.php...fo&userid=2874 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
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 -- Leith Ros ----------------------------------------------------------------------- Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
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 -- Myle ----------------------------------------------------------------------- Myles's Profile: http://www.excelforum.com/member.php...fo&userid=2874 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
Hello Myles, Let's deconstruct example "C" Sub test3() If 4 < 5 Then 'evaluated MsgBox "4<5" If 8 10 Then 'not evaluated MsgBox "810" 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 -- Leith Ros ----------------------------------------------------------------------- Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
How about reposting in a readable format?
Tim "Myles" wrote in message ... Having in times past encountered very freakish behavior of IF...THEN clauses, I now wish to bring up some of my observations for comment and possible explanation. Below are 4 trumped-up cases each showing the evaluation, or lack of it, of the conditions set out in each routine. Cases A and B are straightforward as they conform to what we might expect. Namely, there is indication of serial evaluation of all the conditions- whether met or not. By contrast, Cases C and D betray the norm, at least seemingly so, as 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 *\"43\" IF 8 10 THEN 'EVALUATED IF 4 = 4 THEN MSGBOX \"810\" *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 COND3 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 \"810\" MSGBOX \"810\" IF 3 = 3 THEN *'NOT EVALUATED* IF 3 = 3 THEN *'NOT EVALUATED 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. Myles -- Myles ------------------------------------------------------------------------ Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746 View this thread: http://www.excelforum.com/showthread...hreadid=506655 |
Bizzare behavior of IF..THEN
Hi
It's very confusing, maybe you try to explayn more clearly. In general, possible syntax variations for IF clause a 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? -- Arvi Laanemets ( My real mail address: arvi.laanemets<attarkon.ee ) "Myles" wrote in message ... Having in times past encountered very freakish behavior of IF...THEN clauses, I now wish to bring up some of my observations for comment and possible explanation. Below are 4 trumped-up cases each showing the evaluation, or lack of it, of the conditions set out in each routine. Cases A and B are straightforward as they conform to what we might expect. Namely, there is indication of serial evaluation of all the conditions- whether met or not. By contrast, Cases C and D betray the norm, at least seemingly so, as 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 *\"43\" IF 8 10 THEN 'EVALUATED IF 4 = 4 THEN MSGBOX \"810\" *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 COND3 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 \"810\" MSGBOX \"810\" IF 3 = 3 THEN *'NOT EVALUATED* IF 3 = 3 THEN *'NOT EVALUATED 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. Myles -- Myles ------------------------------------------------------------------------ Myles's Profile: http://www.excelforum.com/member.php...o&userid=28746 View this thread: http://www.excelforum.com/showthread...hreadid=506655 |
Bizzare behavior of IF..THEN
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 -- Myle ----------------------------------------------------------------------- Myles's Profile: http://www.excelforum.com/member.php...fo&userid=2874 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
On Tue, 31 Jan 2006 08:04:21 -0600, Myles
wrote: Hi Ross, In the face of the execution behavior, your explanation sounds plausible. I am however still struggling to come to terms with the logic behind it. Is it not intuitively appealing that excecution should terminate only when a condition evaluates to TRUE? Put another way, conventional reasoning would demand that the code should keep on "searching" past 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 |
Bizzare behavior of IF..THEN
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 -- Myle ----------------------------------------------------------------------- Myles's Profile: http://www.excelforum.com/member.php...fo&userid=2874 View this thread: http://www.excelforum.com/showthread.php?threadid=50665 |
Bizzare behavior of IF..THEN
On Tue, 31 Jan 2006 09:24:36 -0600, Myles
wrote: Hi Ron, you wrote inter alia: 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. Since the code exits upon encountering a FALSE evaluation, could you please give an illustration of how you can base your tests on -a consequence of the false result- Thanks 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 ("810") 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 ("810") ElseIf 3 = 3 Then MsgBox ("3=3") Else MsgBox ("other") End If End Sub ================================= --ron |
All times are GMT +1. The time now is 06:58 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com