Forums Register Login

Expression instead of condition in if block

+Pie Number of slices to send: Send
I know that in general we write a condition in an if block but...can we write an expression in an if block as in the following code..
Assume the code is inside main method.


This piece of code is from a scjp questionaire posted on the following link (question 2)
scjp
+Pie Number of slices to send: Send
[Not-so-nice response removed. While we encourage people to put some effort into their posts, there are nice ways to put it, and not-so-nice ways. Be sure to use the former.]
+Pie Number of slices to send: Send
Yes we can write expression in if block as long as it is evaluating to a boolean.



+Pie Number of slices to send: Send
Thanks Anuj....
+Pie Number of slices to send: Send
I would regard what the question says as a logic error, but then SCJP questions tend to include weird code which one would never use in real life. Similarly, == false and == true count as poor style.

The correct way to write that expression would have been

if (b2 && b1) . . .
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 802 times.
Similar Threads
confused about the answer
Explain me this Programm
It should be true.........
assignment operator?
logical operator doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:09:44.