Click to See Complete Forum and Search --> : Shortcircuit OR


Jenda
September 17th, 2001, 08:51 AM
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

Cakkie
September 17th, 2001, 09:14 AM
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
slisse@planetinternet.be

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

Jenda
September 17th, 2001, 09:23 AM
Silly stuff. Did MS think shortcircuit conditional operators are too hard to understand for the dummies?

Thanks anyway, Jenda

Boumxyz2
September 17th, 2001, 09:31 AM
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

Jenda
September 17th, 2001, 09:41 AM
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

DSJ
September 17th, 2001, 09:57 AM
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!

Jenda
September 17th, 2001, 10:16 AM
Doesn't work. Try :


msgbox iif( true, 1, 2/0)




:-(
Jenda

Cimperiali
September 18th, 2001, 05:26 AM
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

Cakkie
September 18th, 2001, 05:47 AM
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
slisse@planetinternet.be

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

Jenda
September 18th, 2001, 05:59 AM
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.