Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Question from K&B Book

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
this question is from pg 200 Question 12 from K&B book

the ans is C. count = 3
when i first do the question, i get count = 4, the reason being i evaluated this code first, (++count - 2).
however the explanation is that it is a short ciruit expression thus, the left-hand side will be evaluated first, since it evaluated to false the right hand expression would not be executed.
So my question is this, arent expression in () always evaulated first? or am i mistaken?

blacksmith
Posts: 979
• Number of slices to send:
Optional 'thank-you' note:
Hi,
The variable count is equal to 3 before the if expression
is evaluated, so the short circuit operator rule that the
right hand side is not evaluated holds in this case as
well. Please replace the void test() method with:

Running the main will result in:
C:\projects\misc>java BoolArray
b[0] = true
b[1] = false
b[2] = true
Before If count = 3
After If count = 3

Cheers,
Gian Franco Casula

Ranch Hand
Posts: 40
• Number of slices to send:
Optional 'thank-you' note:
I have changed the code a bit:

The first condtion gets excuted.
b[0] = true
b[1] = false
so b[0] && b[1] = false
then b[2] = true
so b[0] && b[1] | b[2] = true
and thus count gets incremented

Shilpi M Ag
Ranch Hand
Posts: 40
• Number of slices to send:
Optional 'thank-you' note:
In addtion to what i said above:

in this bit of code cince b[0] == false, the compiler does not go ahead

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
In general they are, but when used in combination with &&, || and ?: this is not the case.
The Java Language Specification has a very clear section on this. You could check out paragraph 15.7 of the JLS. It's a very short and not so technical explanation.

guan di
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
actaully, i do know the short circuit rule, but i mistakenly thought that code in () will always be executed first....
thanks for the replies and explanations, are there any other cases beside short-circuit logical expressions where code in () are not executed first?

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
If you have, for example,

the (++i) and (i++) would not get evaluated first. The "j = ..." expression is evaluated left to right.
So, in this case, the i++ returns 0, but sets i to 1. The (++i) returns and sets i to 2. The (i++) returns 2 and sets i to 3. This is another case where code in () does not get evaluated first.
[ May 08, 2004: Message edited by: Jong Limb ]

 I'm all tasted up for a BLT! This tiny ad wants a monte cristo! We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser