combined conditional expressions question
Hello, I would like to verify if my explanations here are correct. ( I believe they are, after some experimental tests, but just in case)
In a XOR statement, both expressions are evaluated, but are the alterations to the x variable permanent, even if the if statement is false and hence doesnt ever run? I mean there could be a complicated boolean condition that alters variable x for the if statement to be evaluated. When/if that statement eventually evaluates to false, the expression following the if will never run... But is the variable x permanently altered? Say, for code that follows later in the source file?-- So when conditionals are evaluated, regardless of the outcome (tru or false) any alterations to variables remain. I think thats true. Thanks for any input.
publicclass Zeta{
publicstaticvoid main(String[] args){
int x=1;
if((4>x)^((++x+2)>3)) x++;
System.out.println("First if: " + x);
if((4>++x)^!(++x==5)) x++;
System.out.println("Second if: " + x);
System.out.println(x);
}
}
[1505 byte] By [
DeChristoa] at [2007-11-26 20:18:28]

I don't know what you're saying.If you change a variable, it's changed. It doesn't magicall go back to its earlier value just because it's used in a boolean expression that evaluates to false. But then, you could confirm this quite easily yourself by just testing it.
Deco,
Yep XOR == (b1||b2) && (!(b1&&b2)) so both expressions must be evaluated, and
Yep ++x "permanently" changes the value of x.
Also watch the indentation in your code... white space is not significant to the compiler but it is significant to the (well trained) programmer.
> Hello, I would like to verify if my explanations here
> are correct. ( I believe they are, after some
> experimental tests, but just in case)
>
> In a XOR statement, both expressions are evaluated,
> but are the alterations to the x variable permanent,
> even if the if statement is false and hence doesnt
> ever run? I mean there could be a complicated boolean
> condition that alters variable x for the if statement
> to be evaluated. When/if that statement eventually
> evaluates to false, the expression following the if
> will never run... But is the variable x permanently
> altered? Say, for code that follows later in the
> source file?-- So when conditionals are evaluated,
> regardless of the outcome (tru or false) any
> alterations to variables remain. I think thats true.
> Thanks for any input.
If you change the variable's value in the process of evaluating a conditional then yes, that value will remain. However, XOR does not change the variable's value. The only thing I see modifying variables is your prefix and postfix unary operators.
jverd... it's a clumsily asked... "Does java short-circuit?" to which the answer is "Yes, but it can't short-circuit XOR coz it's involves a implicit &&.
> jverd... it's a clumsily asked... "Does java
> short-circuit?" to which the answer is "Yes, but it
> can't short-circuit XOR coz it's involves a implicit
> &&.
I'm not so sure but that may be the case. It almost seemed to be more about whether or not the variable would still be changed if the entire conditional evaluated to false.
Thanks, yes I guess thats true....just wasnt so straightforward to me... Another confusion that I have about conditionals, is if they are evaluated right to left, I mean is there any priority or precedence?
(expression1)&(expression2)|(expression3)^(expression4)&&(expression5)
Would that be evaluated pair by pair just from right to left? Thanks in advance.
by the way I appreciate all the answers... I just formulated the question best I knew how...
<google-replacement-therapy class="advertisement">
google is your friend... google google google.
google "java operator precedence".
nice cheat sheet at http://www.uni-bonn.de/~manfear/javaoperators.php
nice explanation at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
</google-replacement-therapy>
Good suggestions.... Thanks Corlettk