Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. And vs AndAlso, Or vs OrElse

And vs AndAlso, Or vs OrElse

Scheduled Pinned Locked Moved Visual Basic
questionvisual-studiotutorial
17 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ninkk
    wrote on last edited by
    #1

    Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance

    C D C 3 Replies Last reply
    0
    • N ninkk

      Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      D P M 3 Replies Last reply
      0
      • N ninkk

        Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance

        D Offline
        D Offline
        Duncan Edwards Jones
        wrote on last edited by
        #3

        AndAlso and OrElse perform what is known as short curcuit evaluation. What this means is that if the first argument makes it such that the result is known then the second argument is not tested. For example if you have:

        If DayIsTuesDay() And DayisSomeonesBirthday()

        Both DayIsTuesday() and DayIsSomeonesBirthday() will be valuated. However if you have:

        If DayIsTuesDay() AndAlso DayisSomeonesBirthday()

        and DayIsTuesday returns false then there is no point executing DayIsSomeonesBirthday Where your second operation is doing something slow (like reading from a database) using short curcuit evaluations can significantly speed up your application.

        '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

        C 1 Reply Last reply
        0
        • N ninkk

          Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          I think the subliminal message is: "use C#!"

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          D 1 Reply Last reply
          0
          • D Duncan Edwards Jones

            AndAlso and OrElse perform what is known as short curcuit evaluation. What this means is that if the first argument makes it such that the result is known then the second argument is not tested. For example if you have:

            If DayIsTuesDay() And DayisSomeonesBirthday()

            Both DayIsTuesday() and DayIsSomeonesBirthday() will be valuated. However if you have:

            If DayIsTuesDay() AndAlso DayisSomeonesBirthday()

            and DayIsTuesday returns false then there is no point executing DayIsSomeonesBirthday Where your second operation is doing something slow (like reading from a database) using short curcuit evaluations can significantly speed up your application.

            '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

            C Offline
            C Offline
            Chinners
            wrote on last edited by
            #5

            You may have to be a bit careful though. In the statement

            If DayIsTuesDay() And DayisSomeonesBirthday()

            if "DayIsTuesday" alters data, and "DayIsSomeonesBirthday" also alters data, you might not want to short-cut... for example (and yes, its a crap example!)

            if ClearUserTable() and ClearPasswordTable() then
            messagebox.show("Tables Cleared")
            end if

            In this case, you would definitely NOT want to shortcut the second function using AndAlso

            1 Reply Last reply
            0
            • C CPallini

              I think the subliminal message is: "use C#!"

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              D Offline
              D Offline
              Duncan Edwards Jones
              wrote on last edited by
              #6

              The VB.Net keyword And is equivalent to the C# operator & The VB.Net keyword AndAlso is equivalent to the C# operator && The VB.Net keyword Or is equivalent to the C# operator | The VB.Net keyword OrElse is equivalent to the C# operator || The subliminal message is RTM[^]?

              '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

              C L M 3 Replies Last reply
              0
              • C Christian Graus

                Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.

                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                D Offline
                D Offline
                Duncan Edwards Jones
                wrote on last edited by
                #7

                This is in no way different from the way C# works with &[^] and &&[^]...

                '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

                C 1 Reply Last reply
                0
                • D Duncan Edwards Jones

                  The VB.Net keyword And is equivalent to the C# operator & The VB.Net keyword AndAlso is equivalent to the C# operator && The VB.Net keyword Or is equivalent to the C# operator | The VB.Net keyword OrElse is equivalent to the C# operator || The subliminal message is RTM[^]?

                  '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Duncan Edwards Jones wrote:

                  The subliminal message is RTM[^]?

                  Nope. IMHO default behaviour should be short-cut and the keywords AndAlso, OrElse are simply foolish, hence my subliminal still stands. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • D Duncan Edwards Jones

                    The VB.Net keyword And is equivalent to the C# operator & The VB.Net keyword AndAlso is equivalent to the C# operator && The VB.Net keyword Or is equivalent to the C# operator | The VB.Net keyword OrElse is equivalent to the C# operator || The subliminal message is RTM[^]?

                    '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Right. Read The Fantastic Manual. Apparently no one does anymore, even asking Google seems too much for some. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Voting for dummies? No thanks. X|


                    P 1 Reply Last reply
                    0
                    • C Christian Graus

                      Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.

                      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                      P Offline
                      P Offline
                      Paul Conrad
                      wrote on last edited by
                      #10

                      Nice explanation about AndAlso. However, proper checks for the division by zero in the example could do away with the need for AndAlso. It does make sense about short circuiting if the boolean condition can be determined with out fully evaluating the entire expression.

                      "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                      1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Right. Read The Fantastic Manual. Apparently no one does anymore, even asking Google seems too much for some. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Voting for dummies? No thanks. X|


                        P Offline
                        P Offline
                        Paul Conrad
                        wrote on last edited by
                        #11

                        Luc Pattyn wrote:

                        Google seems too much for some

                        Seems like it.

                        "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                        1 Reply Last reply
                        0
                        • D Duncan Edwards Jones

                          This is in no way different from the way C# works with &[^] and &&[^]...

                          '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #12

                          The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ).

                          Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                          J 1 Reply Last reply
                          0
                          • D Duncan Edwards Jones

                            The VB.Net keyword And is equivalent to the C# operator & The VB.Net keyword AndAlso is equivalent to the C# operator && The VB.Net keyword Or is equivalent to the C# operator | The VB.Net keyword OrElse is equivalent to the C# operator || The subliminal message is RTM[^]?

                            '--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd

                            M Offline
                            M Offline
                            Mark Churchill
                            wrote on last edited by
                            #13

                            As a minor semantic difference - the & operator in C# is a bitwise and, whereas && is the logical and which also shortcircuits. (Like C++) I'm used to using & with caution - coming from a C++ background - you could have two "true" results from a function - but 1 & 2 == false ;) In C# this cautioning really doesnt apply though.

                            Mark Churchill Director Dunn & Churchill Free Download:
                            Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                            D 1 Reply Last reply
                            0
                            • M Mark Churchill

                              As a minor semantic difference - the & operator in C# is a bitwise and, whereas && is the logical and which also shortcircuits. (Like C++) I'm used to using & with caution - coming from a C++ background - you could have two "true" results from a function - but 1 & 2 == false ;) In C# this cautioning really doesnt apply though.

                              Mark Churchill Director Dunn & Churchill Free Download:
                              Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                              D Offline
                              D Offline
                              Dave Doknjas
                              wrote on last edited by
                              #14

                              There is no semantic difference. The following are *exactly* equivalent: VB: C#: And & Or | AndAlso && OrElse || The VB 'And' is *both* a bitwise operator and a non-short-circuiting logical operator, as is the C# '&' operator. Ditto for 'Or' and '|'. They produce identical results when used in the same situations. Obviously, it usually makes sense to use 'AndAlso' and 'OrElse' in most logical evaluation situations, but occasionally 'And' and 'Or' may be preferred, in exactly the same cases where '&' and '|' may be preferred in C#.

                              David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI

                              1 Reply Last reply
                              0
                              • C Christian Graus

                                The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ).

                                Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                                J Offline
                                J Offline
                                Jon_Boy
                                wrote on last edited by
                                #15

                                >>The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ). Correct! VB6 would eval both expressions before coming back with a result, even if the first failed. AndAlso & OrElse allow "short cut" checks. Meaning after the first expression that fails/passes the check, the remaining checks are ignored and processing continues.

                                1 Reply Last reply
                                0
                                • C Christian Graus

                                  Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.

                                  Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                                  M Offline
                                  M Offline
                                  Mark Churchill
                                  wrote on last edited by
                                  #16

                                  The one that drives me nuts is the lack of a decent ternery operator. The rough equivalent of say: foo == null ? "null" : foo.bar; is Iif(foo = null, "null", foo.bar) Which is just a regular function, all arguments get evaluated before it's called, and bang! Null ref. :/

                                  Mark Churchill Director Dunn & Churchill Free Download:
                                  Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                                  C 1 Reply Last reply
                                  0
                                  • M Mark Churchill

                                    The one that drives me nuts is the lack of a decent ternery operator. The rough equivalent of say: foo == null ? "null" : foo.bar; is Iif(foo = null, "null", foo.bar) Which is just a regular function, all arguments get evaluated before it's called, and bang! Null ref. :/

                                    Mark Churchill Director Dunn & Churchill Free Download:
                                    Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                                    C Offline
                                    C Offline
                                    Christian Graus
                                    wrote on last edited by
                                    #17

                                    Yeah, I've been bitten by that on brief visits to VB land

                                    Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups