# return statement

Hi,

I am going through below challenge solution

http://codingbat.com/prob/p140449

I have not understood below return statement.

return (talking && (hour < 7 || hour > 20));

public boolean parrotTrouble(boolean talking, int hour) {
return (talking && (hour < 7 || hour > 20));
// Need extra parenthesis around the || clause
// since && binds more tightly than ||
// && is like arithmetic *, || is like arithmetic +
}
LVL 7
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
&& is  Conditional-AND;  false when left operand (talking) is false, otherwise evaluate the second operand
|| is Conditional-OR;  true when the left operand (hour < 7) is true, otherwise evaluate the second operand (hour > 20)

Without the parentheses, talking && hour < 7 || hour > 20 would evaluate as ((talking && hour < 7) || hour > 20)

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
While I don't understand the coding logic (why it returns, for what reason), interpreting the code is simple.

parrotTrouble(True, 1) = True
parrotTrouble(True, 8) = False
parrotTrouble(True, 21) = True
parrotTrouble(False, 1) = False
parrotTrouble(False, 8) = False
parrotTrouble(False, 21) = False

It just returns the AND of the two variables. Talking is either True or False, it's already input as the first variable.
The second part is evaluated from the second variable. If hours is below 7 OR above 20, that second part is True (otherwise False).
With these two outcomes, you do the AND operation again for the final result.
Commented:
>>talking && (hour < 7 || hour > 20)

Quite simply - talking has to be true, and hour has to be EITHER less than 7 OR more than 20, then a final true is returned - otherwise false is returned of course.
Commented:
Thanks.

Footnote : (now you've done the challenge) -

This would be a way of splitting the logic up also :

``````public boolean parrotTrouble(boolean talking, int hour) {

if (talking==false) {return talking;}
return (hour<7||hour>20);

}
``````
Commented:
//If you're going to split it up
if( !talking ){ return false; }
if( hour<7 ){ return true; }
return hour>20;
Commented:
You can do that too of course, but I was sticking to the illustration of keeping the ORs together.
Commented:
And don't forget our old friend Mr Ternary

``````return talking = talking==true?hour<7||hour>20?true:false:false;
``````
Commented:
This might also be an option at some point :

``````if ((!(talking==false))&& !((hour>6)&&(hour<21))){return true;}
else{return false;}
``````

- which is just for people who don't like ors. ;)

OR should it be :

``````if ((!(talking==false))&& !((hour>6)&&(hour<21))){return !false;}
else{return !true;}
``````
Commented:
Presumably those are just meant to be silly, but
!((hour>6)&&(hour<21))
is not equivalent to
(hour < 7 || hour > 20)
Commented:
They are not silly inasmuch as they all pass the codingbat tests - so maybe the tests are silly.
Commented:
Remember that >6 and <21 are the hours that ARE ok to speak.

(and so for the record, your ID: 40411496 is not correct).

You can look at it this way :
"<x OR >y" MUST be the complement of ">=x AND <=y".
Commented:
Sorry, I misread "!((>x-1)&&(<y+1))", expecting  ">=x AND <=y" (one of the reasons to avoid silly extraneous transforms)
!((hour>6)&&(hour<21)) is equivalent when hour is an int
Commented:
Right, ok.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.