gudii9
asked on
comparing sign of two numbers
Hi,
I was going through below link
http://codingbat.com/prob/p192082
I wrote solution as below
public boolean icyHot(int temp1, int temp2) {
if(temp1<0 && temp2>100)
{
return true;
}
if(temp1>0 && temp2<100)
{
return true;
}
return false;
}
I saw given solution as below
public boolean icyHot(int temp1, int temp2) {
if ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0)) {
return true;
} else {
return false;
}
// Could be written as: return ((temp1 < 0 && ...));
}
can the natural flow of next line can be written always after || (which is the only difference i see from my program.
Please advise
I was going through below link
http://codingbat.com/prob/p192082
I wrote solution as below
public boolean icyHot(int temp1, int temp2) {
if(temp1<0 && temp2>100)
{
return true;
}
if(temp1>0 && temp2<100)
{
return true;
}
return false;
}
I saw given solution as below
public boolean icyHot(int temp1, int temp2) {
if ((temp1 < 0 && temp2 > 100) || (temp1 > 100 && temp2 < 0)) {
return true;
} else {
return false;
}
// Could be written as: return ((temp1 < 0 && ...));
}
can the natural flow of next line can be written always after || (which is the only difference i see from my program.
Please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you please clarify your question?
ASKER
public boolean icyHot(int temp1, int temp2) {
if( (a) || (b) ){
return true;
}
}
why above code wont compile? Giving return statement in if loop alone is not sufficient. Do i need to give outside if loop also?
please advise.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Because you didn't define a and b
(The equivalent form would also not compile for equivalent reason)
I see no loop in your code, so I don't understand your last comment.
(The equivalent form would also not compile for equivalent reason)
I see no loop in your code, so I don't understand your last comment.
Your original solution is fine and just as good as the "given solution" on the website.
As you said, the only difference is that they put the two conditions together with an "||" (or) and you broke them out into separate lines. Either approach is fine.
I think you misunderstood what ozo posted. He was basically saying that the two solutions are equivalent (he just said it in code).
Doug
As you said, the only difference is that they put the two conditions together with an "||" (or) and you broke them out into separate lines. Either approach is fine.
I think you misunderstood what ozo posted. He was basically saying that the two solutions are equivalent (he just said it in code).
Doug