how can i show below expressions in java? a = 20; b = 0; c = a || b++; c = a && b ++; c = a && ++b;
|| is or, and && is and.c = a || b++ doesn't really do anything as your'e saying c = a or b++....
i wanted to show that when we use || operator , if the first expression is true , then we don't check the second expression, so b won't change and when we use && operator , if the first expression is true , then we should check the second too , so the b's value will change!
I'd probably use a couple of if-statements and it's also more pedagogic to use compare instead of assignment in the first term. The first would look something like this: int a = 2, b = 0; if (a == 2 || b++ ) System.out.println("b = "+ b)
if(a == something || b == something) { //do something } or what you seem to be thinking of is a conditional output. for example c = (a > b) ? a : b; this is saying if a > b then a is true, else b is true, and set that = to c. if is the same as if(a > b) { c = a } else if (b>a) // or just else. { c = b }
Also if, then, else, else if, while, do-while, break, etc are all cross language, so you should be semi-familiar with how to do this if you have another language background.
Join our real-time social learning platform and learn together with your friends!