Solved

# comparing sign of two numbers

Posted on 2014-10-02
102 Views
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.

0
Question by:gudii9

LVL 84

Accepted Solution

ozo earned 300 total points
if( a ){
return true;
}
if( b ){
return true;
}
return false;

is equivalent to

if( (a) || (b) ){
return true;
}else{
return false;
}
0

LVL 74

Expert Comment

0

LVL 7

Author Comment

``````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?
0

LVL 74

Assisted Solution

käµfm³d   👽 earned 200 total points
Because you don't have a return statement outside of the if block. Think about it this way:  If your code never entered the if block, what would it return? We cannot tell because you have not defined anything.
0

LVL 84

Expert Comment

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.
0

LVL 26

Expert 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
0

## Featured Post

This article will show, step by step, how to integrate R code into a R Sweave document
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…