CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Shortcircuit OR

  1. #1
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Shortcircuit OR

    Is there any way to tell the [CENSORED] pseudolanguage called Visual Basic that if the result of the first ORed condition is TRUE that I do NOT want it to try to evaluate the other?

    I have a code like this :

      Do While Not (rst.EOF _
          Or rst.Fields("fieldtitle") = "_#_split_output_file_#_")
        strColumn = strColumn & Trim(rst.Fields("FIELDTITLE")) & delimiter
        rst.MoveNext
      Loop

    Of course if the rst.EOF is TRUE, rst.Fields("...") will lead to an error.

    For the time being I'll convert the code to something perverted
    (read VBish), but this is something I'd really like to know.

    Thanks, Jenda



  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Shortcircuit OR

    This is what MSDN says about it:

    -MSDN-
    Conditionally executes a group ofstatements, depending on the value of an expression ... If the condition exists out of more than one expression, due to combinations of operators (like AND, OR,...) the entire expression is evaluated, resulting in every part of the expression being evaluated.
    -MSDN-

    I thing MSDN is pretty clear about this. If you really want that, you must split the IF statement into multiple If statements.

    Dim bln as Boolean
    bln = SomeExpressionGivingABooleanValueAsReturn
    If Not(bln) then bln = SomeOtherExpressionGivingABooleanValueAsReturn
    If bln then
    ' code when at least one of the two results in true
    else
    ' code when both conditions evaluate to false
    End If





    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Shortcircuit OR

    Silly stuff. Did MS think shortcircuit conditional operators are too hard to understand for the dummies?

    Thanks anyway, Jenda


  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Shortcircuit OR

    Why don't you try nested condition ?

    or you can try to check the Reversed condition

    If Not EOF and Not Your condition

    Then if this is true then do your false code here
    and in the else do your true code.

    Nicolas


    Nicolas Bohemier

  5. #5
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Shortcircuit OR

    The reversed condition would lead to the error as well.

    And well ... is not


    If cond1 And cond2 then
    ...
    end if




    better than


    If cond1 then
    If cond2 then
    ...
    end if
    end if




    ? But since the first is aparently not possible, this discussion is academic :-(((

    Jenda


  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Shortcircuit OR

    This should work...


    While not (iif(rs.eof, true, rst.Fields("fieldtitle") = "_#_split_output_file_#_"))

    Loop





    I agree though... a compiler switch or something to turn on/off short-cutting would be nice!




  7. #7
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Shortcircuit OR

    Doesn't work. Try :


    msgbox iif( true, 1, 2/0)




    :-(
    Jenda


  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Shortcircuit OR

    Despite all, you should notice C, C++ has a similar strange effect in evaluation code inside a switch statement (the vb Select Case). While Vb test each condition till it reach the true one and then skipp all other test, the C and C++ go on and on unless you put a break clause inside each Case... So, do not ask us why, but it seems it is popular to have to code some more lines (or to split if test, in this case) due to strange assumptions of those who wrote the code language...
    (Just to say "Vb is what I like most")
    ;-)
    Regrads,
    Cesare

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Micahel
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Shortcircuit OR

    Another strange thing abou thte C switch statement is that when a condition is met, and there is no code in the block, it will take the code of the next block. Take this select case

    Select Case 1
    Case 1
    Case 2
    Msgbox "2"
    Case else
    Msgbox "Unknown"
    End Select



    Run this, and you will see that nothing happens, run this in C, and you will see a messagebox saying "2", because the 1 block is empty

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  10. #10
    Join Date
    Sep 2001
    Location
    Prague, Czech Republic
    Posts
    43

    Re: Shortcircuit OR

    Well I can understand the semantics of C switch statement. The fact that unless you "break" you fall down to the next case may actualy be helpfull. Especialy with code like Cakkie posted with empty cases.

    But I do not understand why would anyone want to evaluate all conditions even though their value is not needed. See ... the only time when this makes any difference is when there is a side-effect of the evaluation (which is something you should not do anyway since it often leads to errors) or when the evaluation leads to an error.

    Jenda

    P.S.: I don't like VB at all. But I hate C as well. Both are incredibly badly designed languages. IMHO of course.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured