• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Confused??

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

why it prints 3 here. I thought is shuould be 1,



Thanks in advance.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well,guys
First,I think you confused && with ||
if you use ||,the first statement is right,the second is not executed,but,to &&,the result is not that!
best wishes!



yours
Taihua
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi,
I think you have got the answer.Dig more deeper by changing & with | and && with ||.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjeev,

I actually know this long before and sometimes I get dugged with these kind of simple questions. I dont know what to do...Anyways thaks for helping this out for me.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjeev,

I actually know this long before and sometimes I get dugged with these kind of simple questions. I dont know what to do...Anyways thaks for helping this out for me. So the point is with the && opertor, if the first operand eveluates to true, it checks for the other operand as well and that should be true in order for the condition to be true. To the contrary, if the first operand evaluates to false, then && wont check for the second operand and the result of the entire expression is false.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so Jothi I guess now you understood why the answer is 3
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jothi,
I think you have got the key difference between the two operators anyway try this

This is a K&B book problem ,revisit it!
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sanjeev,

I got it now. Sometimes, I tend to forget them. Anyways thanks all ranchers for the help.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short circuits, && and ||

A && B
(A AND B) can short circuit only if A is false. Then the whole expression will be false, B has not to be tested.
If A is true, B has to be tested as well, cause if B is false, the whole expresion would still be false.


A || B
(A OR B) can short circuit only if A is true. Because regardless of B the whole expression would be true.


A ^^ B does not exist
A short circuit of A XOR B cannot work...


A&B or A|B are without short circuit, both A and B are tested in any case.




Yours,
Bu.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic