|
From: Al Dunbar on 5 Jul 2008 01:24 "Richard Mueller [MVP]" <rlmueller-nospam(a)ameritech.nospam.net> wrote in message news:eBtb1GY3IHA.3480(a)TK2MSFTNGP03.phx.gbl... > > "preet" <preetkanwaljit> wrote in message > news:eI9RTXV3IHA.5060(a)TK2MSFTNGP02.phx.gbl... >> Is there a better way to handle multiple IF Conditions like >> >> >> IF condition1 AND condition2 AND condition3 AND .... THEN >> >> code >> >> END IF >> >> >> Now suppose i have 30 to 40 conditions to test, how do i handle this >> snippet in an easier manner. >> >> Guide > > I don't think there is a better way. In theory if you know that one or a > few of the conditions are more likely to be false, the code could be more > efficient if you nested the If statements, testing the ones likely to be > false first. Then the remaining conditions are seldom evaluated. For > example, if condition1, condition2, and condition3 are more likely to be > false than the others: > > If condition1 Then > If condition2 Then > If condition 3 Then > If condition4 And condition5 And condition6 Then > ' ... code. > End If > End If > End If > End If > > Even here I doubt you could tell the difference, even with 40 conditions. There is a way to reduce the nesting from any number of levels to one: a = "a" b = "b" c = "ZZZ" do if not a = "a" then exit do if not b = "b" then exit do if not c = "c" then exit do wscript.echo "test1: all conditions are true" loop until true c = "c" do if not a = "a" then exit do if not b = "b" then exit do if not c = "c" then exit do wscript.echo "test2: all conditions are true" loop until true If you need an "else" clause, then: a = "a" b = "b" c = "ZZZ" do test1 = false if not a = "a" then exit do if not b = "b" then exit do if not c = "c" then exit do test1 = true loop until true if test1 then wscript.echo "all conditions were true" else wscript.echo "at least one conditions was false" end if /Al > -- > Richard Mueller > MVP Directory Services > Hilltop Lab - http://www.rlmueller.net > -- > >
From: Dr J R Stockton on 5 Jul 2008 11:32 In microsoft.public.scripting.vbscript message <eBtb1GY3IHA.3480(a)TK2MSFT NGP03.phx.gbl>, Thu, 3 Jul 2008 21:57:19, "Richard Mueller [MVP]" <rlmueller-nospam(a)ameritech.nospam.net> posted: >I don't think there is a better way. In theory if you know that one or a few >of the conditions are more likely to be false, the code could be more >efficient if you nested the If statements, testing the ones likely to be >false first. Not necessarily; some conditions take more work than others. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
First
|
Prev
|
Pages: 1 2 Prev: Handling Events in a VBScript Class Next: wshremote not running script on remote computer |