Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Multiple "If - End If" question

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,29898
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Multiple "If - End If" question
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Multiple "If - End If" question - 1/14/2006 10:37:58 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Is there a way to avoid this:

...
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
...

I mean when there are lots of IF's and ELSE's isn't there a way to avoid the  long column of END IF's at the end?

_____________________________

Fred
 
 
Post #: 1
 
 RE: Multiple "If - End If" question - 1/14/2006 4:52:55 PM   
  Cybex


Posts: 412
Score: 0
Joined: 9/14/2005
From: Florida
Status: offline
Not really...

If you use "IF" statments that is the only way that I know to close them, one at a time...

Cybex

_____________________________

Common sense is not so common.

(in reply to Fredledingue)
 
 
Post #: 2
 
 RE: Multiple "If - End If" question - 1/15/2006 11:45:30 PM   
  ginolard


Posts: 1068
Score: 21
Joined: 8/10/2005
Status: offline
As a general rule if you are testing something that requires multiple IF statements (more than 2 say), it is infinitely preferable to use the SELECT/CASE method.  That may or may not be suitable in your case though as you didn't post the whole code ;)

(in reply to Cybex)
 
 
Post #: 3
 
 RE: Multiple "If - End If" question - 1/16/2006 3:12:31 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
thats what code blocks are for, i always tab my blocks over else id never be able to read even my own code lol

_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ginolard)
 
 
Post #: 4
 
 RE: Multiple "If - End If" question - 1/16/2006 4:57:53 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Thanks for you response!

In fact, select case doesn't apply for my code but I will think of it in the future.
The problem is that I need this (but much longer)


      

This works:


      

but not this :(  :


      

< Message edited by Fredledingue -- 1/16/2006 4:59:40 AM >


_____________________________

Fred

(in reply to kirrilian)
 
 
Post #: 5
 
 RE: Multiple "If - End If" question - 1/16/2006 5:40:17 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Fredledingue,

your last example will work, if you change it like this:


      

(you must select/compare your expressions with True)

(in reply to Fredledingue)
 
 
Post #: 6
 
 RE: Multiple "If - End If" question - 1/16/2006 6:17:24 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Ho! Hey! Great! That wroks! Thanks!

With my example that gives that:

      

_____________________________

Fred

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Multiple "If - End If" question - 1/16/2006 5:34:29 PM   
  ginolard


Posts: 1068
Score: 21
Joined: 8/10/2005
Status: offline
I swear to God, Select Case True is one of the most underrated commands in the whole of VBscript.  So, so useful is so many circumstances.

(in reply to Fredledingue)
 
 
Post #: 8
 
 RE: Multiple "If - End If" question - 1/17/2006 5:44:24 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
That's very good !! 'Select Case True' is completely new for me. Excellent example.
Thank you Fredledingue for posting this question. 

_____________________________

Regular Expression ? I (L+o{1,}v{1,3}e\s)+[iI]t!$

(in reply to ginolard)
 
 
Post #: 9
 
 RE: Multiple "If - End If" question - 1/17/2006 6:53:32 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
To deal with 'multiple choice conditions' (like fredledingue's example) VBScript offers
a very standard language construct found in most other languages too: the If ... Then ...
ElseIf ... Then ... ElseIf ... Then ... Else ... End If sequence. There is no need for a
long sequence of End Ifs if you use it properly. 
 
The Select Case statement (like switch in other languages) is meant to compare
one value against a number of alternatives.

If you like the "select case true" curiosity of VBScript so much, I challenge you:

Look at this code


      

whether  the

    WScript.Echo "   Condition ('a' = sTest or 'b' = sTest) is true for '" + sTest + "'" 

will print in

    (a)  the If
    (b)  the Select Case

code before running the script.

(in reply to didorno)
 
 
Post #: 10
 
 RE: Multiple "If - End If" question - 1/17/2006 7:50:47 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Hi ehvbs, my guess is that both approaches will not print  "   Condition ('a' = sTest or 'b' = sTest) is true for '" + sTest + "'" 
But, in case of the select case, it is much easier to determine.
By the way, you stick to the use of '+' instead of  '&' for strings (little bit confusing).

Bye !

Edited : After running , I see both methods the same response.

< Message edited by didorno -- 1/17/2006 7:54:42 AM >


_____________________________

Regular Expression ? I (L+o{1,}v{1,3}e\s)+[iI]t!$

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: Multiple "If - End If" question - 1/17/2006 9:30:45 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Thanks ehvbs!

I always had errors with "ElseIf" before because I didn't know that the last "Else" should be without "If".

Now, what do you think of the two methods (ElseIf or Select Case) is faster? Or it doesn't matter?

I ask that because lots of If's in loops increase the processing time quit a lot.

_____________________________

Fred

(in reply to didorno)
 
 
Post #: 12
 
 RE: Multiple "If - End If" question - 1/18/2006 6:35:57 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi didorno,

ad "select case true"

I think you didn't guess for the ElseIf part of the question - you knew what
to expect from the established semantic of this construct. Even Mr. Gates would
have to define a new keyword like "ElseIfOr" or "CheckContinue" (a kind of
reversed break) if he wanted to establish a new conditional statement that
tests/executes further after a first match.

For the Select Case question I know from the Docs that

  ... If testexpression matches an expressionlist expression in more
  than one Case clause, only the statements following the first match
  are executed. ...
 
Therefore no guessing needed for the standard usage of Select Case. But there
is no established convention, analogy or documented promise to base a prediction
about the result of the creative use of Select Case True on - you have to guess
(just for fun, I trust) and then to test if you want to use this trick with
confidence.

ad "&"

I have no arguments for my usage of the "&" operator, only excuses:

  (1) When I started programming (BASIC) there was just one operator "+"; when
      I learned Pascal, C and C++ - ditto. Learning that Perl and PHP want "."
      to concatenate strings was easy, not forgetting it when switching languages
      is more difficult. I couldn't feel much attraction to the idea to have
      two string concatenation operators in one language.
     
  (2) When I red about "&" in the Docs I took the statement
         Forces string concatenation of two expressions.
      seriously. I understand this as: Instead of writing explicit casts like
         sFrs = "11" : nSec = 47 : sTxt = sFrs + CStr( nSec )
      you may write shorter
         sTxt = sFrs & nSec   
      without loosing the indication of the fact that something special happens
      here. That seemed a good idea, but wasn't compatible with using "&" for
      all concatenations.
     
  (3) Then I read
         ... However, if only one expression is Null, that expression is
         treated as a zero-length string ("") when concatenated with the other
         expression. ...
      I really don't like "however" in the docs of a programming language.
      Checking the "+" Docs:
         ... If one or both expressions are Null expressions, result is Null..
      Then I knew that I had to use "+" to concatenate strings. I can't risk
      to let concatenations with unexpected Null values pass thru silently.
     
      Let's assume I have to generate form letters with a promise and a condition
      from a database not under my control and look at this code
     

      

      and this output:

            & I'll give you all my money, if & works.
            + I'll give you all my money, if & works.
            & I'll give you all my money
            + No promises - something wicked happened!     


Hi Fredledingue,

as to what is faster, I haven't a clue. If I should guess, I'd assume the If construct is faster,
because the Select Case is more complicated - it needs to handle lists after the Case keywords.
But that is just speculation.
 

(in reply to Fredledingue)
 
 
Post #: 13
 
 RE: Multiple "If - End If" question - 1/19/2006 3:07:22 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
wow what an excellent thread, both for the select case true and `& vs +` for concatenation.




_____________________________

Have you searched here ?
VBScript Fundamentals
My Site

(in reply to ehvbs)
 
 
Post #: 14
 
 RE: Multiple "If - End If" question - 1/19/2006 3:10:16 AM   
  ebgreen


Posts: 5070
Score: 31
Joined: 7/12/2005
Status: online
Yes, I think I may start using + for concatenation. I always knew it was available I just never took the time to understand its pros/cons. since the pros at least break even or potentially outweigh the cons imo, I think I'll start using it so I have one less thing to worry about when I switch back and forth between VBS and Python.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to kirrilian)
 
 
Post #: 15
 
 RE: Multiple "If - End If" question - 1/19/2006 5:47:42 AM   
  didorno

 

Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
Ehvbs, good point ! And excellently explained.

By the way,  'Select Case False' works as well.


      

But not so easy to read the conditions.



Thanks.

< Message edited by didorno -- 1/19/2006 6:01:23 AM >


_____________________________

Regular Expression ? I (L+o{1,}v{1,3}e\s)+[iI]t!$

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: Multiple "If - End If" question - 1/19/2006 10:28:53 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
I noticed that in the Select Case True construct there is no non-conditional alternative. If none of the condition are met, the script skip the select-case.

With End If you can write a simple Else with no condition attached and used for everything that doesn't fit into the other conditions above.

_____________________________

Fred

(in reply to didorno)
 
 
Post #: 17
 
 RE: Multiple "If - End If" question - 1/19/2006 10:34:57 AM   
  ehvbs

 

Posts: 2204
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Fred,

lthere always is "Case Else" - run my 'challenge code' and you will see:

   ....
   Testing 'c' with select case true
      No condition is true for 'c'

Ekkehard

(in reply to Fredledingue)
 
 
Post #: 18
 
 RE: Multiple "If - End If" question - 1/19/2006 7:43:49 PM   
  ginolard


Posts: 1068
Score: 21
Joined: 8/10/2005
Status: offline
Yes indeed, there is always Case Else.  Think of Select/Case as If/Then/Else's bigger, cleverer brother ;)

(in reply to ehvbs)
 
 
Post #: 19
 
 RE: Multiple "If - End If" question - 1/20/2006 2:52:48 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
hmmm... very clever!

So you all prefer "Case" instead of "If", don't you?

_____________________________

Fred

(in reply to ginolard)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Multiple "If - End If" question Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts