CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    26

    Exclamation Can someone please explain...

    These symbols according to java language:

    When and why would you use these:

    1. &
    2. &&
    3. |
    4. ||

    Thanks

  2. #2
    Join Date
    Nov 2008
    Posts
    26

    Re: Can someone please explain...

    I usuall use the && and || in 'IF' statemets to say either this AND either that or eiter this OR either that.

    OR = ||

    AND = &&

    what are the other two?

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can someone please explain...


  4. #4
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: Can someone please explain...

    && and || are binary logical operators, or more specifically - logical binary short circuit operators. That means that it will only evalute the first operand if the result of the evaluation is decided by the first operand.

    For example in a && expression where the first operand evaluates to false, the second operand will not be evaluated, since the expression anyway will evaluate to false.

    Same goes for || if the first operand is true.

    | and & are binary logical operators and bitwise operators. In addition to the material in the link keang provided they are also binary operators. If you use | or & in a binary expressions both operands will be evaluated regardless of the result of the first expression.

    Consider the value of y after these two code-snippets:
    Code:
    int x = 1;
            int y = 2;
            
            if( x == 1 || (y++ == 2))
            {
               
            }
            // y is now 2
    Code:
    int x = 1;
            int y = 2;
            
            if( x == 1 | (y++ == 2))
            {
               
            }
             // y is now 3.
    Cheers,

    Laitinen

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