15.7k views
25 votes
Rewrite the following condition to avoid a possible arithmetic exception:

if (Math.sqrt(x) < 3 && x > 7)

User Tasegula
by
4.7k points

1 Answer

10 votes

Answer:

if (x > 7 && Math.sqrt(x) < 3)

Step-by-step explanation:

The previous condition checks if the square root of x is less than 3, but this would raise an error if x is a value equal to or less than 0.

So, the condition checks for the value of x before evaluating the square root after the AND operation to prevent an arithmetic exception.

User Mark Watkins
by
5.0k points