Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default order of evaluation

I have a statement like this:

If condition1 Or condition2 Or condition3 Then
blah blah
End If

In situations in which condition2 will not evaluate, an error is raised even
if condition1 evaluates to True (at which point conditions 2 and 3 are
irrelevant, so there's no point in attempting to evaluate them).

Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default order of evaluation

All conditions will be evaluated.

--
Regards,
Tom Ogilvy

"Paul Pedersen" wrote in message
...
I have a statement like this:

If condition1 Or condition2 Or condition3 Then
blah blah
End If

In situations in which condition2 will not evaluate, an error is raised
even if condition1 evaluates to True (at which point conditions 2 and 3
are irrelevant, so there's no point in attempting to evaluate them).

Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default order of evaluation

Paul,
A Select Case will stop evaluation when a matching Case is found. So this
will run:

Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case 1 = 1
Case MyObj.Value = 23
Case Else
End Select

End Sub

but this will fail:


Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case MyObj.Value = 23
Case 1 = 1
Case Else
End Select

End Sub

NickHK

"Paul Pedersen" wrote in message
...
I have a statement like this:

If condition1 Or condition2 Or condition3 Then
blah blah
End If

In situations in which condition2 will not evaluate, an error is raised

even
if condition1 evaluates to True (at which point conditions 2 and 3 are
irrelevant, so there's no point in attempting to evaluate them).

Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default order of evaluation

Nick,

Clever work-around to VBA's lack of short-circuit Boolean evaluation.
I'm sure I'll use it sooner or later. To make it work in general might
require the dreaded goto statement (if "blah,blah" is a sizable code
block):

Sub test()
Select Case True
Case 0 = 1: GoTo ifTrue
Case 1 = 1: GoTo ifTrue 'True!
Case 0 = 1 / 0: GoTo ifTrue '0 = 1/0 is never evaluated! So no
div by 0 error
Case Else
MsgBox "Can skip this of course"
End Select
GoTo done
ifTrue:
MsgBox "blah, blah"
done:
MsgBox "rest of program goes here"

End Sub

This is functionally equivalent to:

Sub test2()
If 0 = 1 Then
GoTo ifTrue
ElseIf 1 = 1 Then
GoTo ifTrue
ElseIf 0 = 1 / 0 Then
GoTo ifTrue
Else
MsgBox "Can skip this of course"
End If
GoTo done
ifTrue:
MsgBox "blah, blah"
done:
MsgBox "rest of program goes here"
End Sub

Probably you can come up with a deeply nested if construct to avoid
the gotos (without having to repeat the statements to be executed) but
it is not clear that it would be any more readable.

-John Coleman

On Mar 16, 3:00 am, "NickHK" wrote:
Paul,
A Select Case will stop evaluation when a matching Case is found. So this
will run:

Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case 1 = 1
Case MyObj.Value = 23
Case Else
End Select

End Sub

but this will fail:

Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case MyObj.Value = 23
Case 1 = 1
Case Else
End Select

End Sub

NickHK

"Paul Pedersen" wrote in message

...



I have a statement like this:


If condition1 Or condition2 Or condition3 Then
blah blah
End If


In situations in which condition2 will not evaluate, an error is raised

even
if condition1 evaluates to True (at which point conditions 2 and 3 are
irrelevant, so there's no point in attempting to evaluate them).


Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?- Hide quoted text -


- Show quoted text -



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default order of evaluation

Hmm, waste of processor cycles. Thanks.


"Tom Ogilvy" wrote in message
...
All conditions will be evaluated.

--
Regards,
Tom Ogilvy

"Paul Pedersen" wrote in message
...
I have a statement like this:

If condition1 Or condition2 Or condition3 Then
blah blah
End If

In situations in which condition2 will not evaluate, an error is raised
even if condition1 evaluates to True (at which point conditions 2 and 3
are irrelevant, so there's no point in attempting to evaluate them).

Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default order of evaluation

Thanks.


"NickHK" wrote in message
...
Paul,
A Select Case will stop evaluation when a matching Case is found. So this
will run:

Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case 1 = 1
Case MyObj.Value = 23
Case Else
End Select

End Sub

but this will fail:


Private Sub CommandButton2_Click()
Dim MyObj As Object

Select Case True
Case MyObj.Value = 23
Case 1 = 1
Case Else
End Select

End Sub

NickHK

"Paul Pedersen" wrote in message
...
I have a statement like this:

If condition1 Or condition2 Or condition3 Then
blah blah
End If

In situations in which condition2 will not evaluate, an error is raised

even
if condition1 evaluates to True (at which point conditions 2 and 3 are
irrelevant, so there's no point in attempting to evaluate them).

Is it wrong to assume that conditions will be evaluated in the order in
which they are presented?








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance Evaluation Free Bird Katie Excel Discussion (Misc queries) 1 June 23rd 09 09:26 PM
Formula evaluation Edward New Users to Excel 2 March 31st 09 01:31 PM
Vlookup evaluation Mick[_2_] Excel Worksheet Functions 2 February 25th 08 02:22 PM
dealer evaluation AB Excel Worksheet Functions 1 June 15th 05 02:17 PM
Options Evaluation Michael G. Excel Worksheet Functions 0 January 28th 05 05:41 PM


All times are GMT +1. The time now is 03:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"