|
-
December 4th, 2008, 05:36 PM
#1
Can someone please explain...
These symbols according to java language:
When and why would you use these:
1. &
2. &&
3. |
4. ||
Thanks
-
December 4th, 2008, 05:37 PM
#2
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?
-
December 4th, 2008, 05:58 PM
#3
Re: Can someone please explain...
-
December 5th, 2008, 05:40 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|