# Beginner - question of 'if' statement

• 11-07-2010, 04:44 PM
hayden06f4i
Beginner - question of 'if' statement
so im trying to prevent a division by zero error with an if statement, and im wondering if i can use one if statement or if i need two.

im doing a gas mileage calculator, i have got everything done up to the calculation. what im wondering is if i can doing something like this or not:

Code:

```if (totalmilescounter != 0, totalgallonscounter != 0) { //calulate gas mileage  MPG = totalmilescounter / gallonscounter; } //end if```
• 11-07-2010, 04:57 PM
JosAH
You can divide any number by any other number as long as that any other number isn't zero:

Code:

```if (anyOtherNumber != 0)   result= anyNumber/anyOtherNumber;```
kind regards,

Jos
• 11-07-2010, 04:58 PM
coltragon
Try and compile it.
Add an else statement and you can see for yourself if it works.
• 11-08-2010, 02:16 AM
hayden06f4i
Quote:

Originally Posted by JosAH
You can divide any number by any other number as long as that any other number isn't zero:

Code:

```if (anyOtherNumber != 0)   result= anyNumber/anyOtherNumber;```
kind regards,

Jos

sorry for the delay in getting back to you, your comment took care of it, i only needed to put one of the items into the if in order to avoid the "divide by 0" issue. thanks!
• 11-08-2010, 03:32 AM
To test multiple field in an if statement you can do it that way:

Code:

```if (someVar != 0 && someOther == 0)   ...```
you could also do:

Code:

```if (someVar != 0 || someOther == 0)   ...```
&& means AND
|| means OR

-> "if varA == 0 && varB == 0" Both condition must be met to enter the if.
-> "if varA == 0 || varB == 0" Either condition must be met to enter.

There are several other comparison "keywords" you can use.
• 11-08-2010, 03:36 AM
hayden06f4i
Jack, thank you for this reply, i was wondering if there was some sort of connector to test for 2 things in the if statement, i tried a comma separating them, a semicolon, tried parenthesis numerous different ways too. Im glad to know that the && or || will work, i will definitely need that in the future! thanks.
• 11-08-2010, 03:45 AM
```if (xyz.x <= t.topRightCorner.x &&                         xyz.x >= t.botLeftCorner.x &&                         xyz.y <= t.topRightCorner.y &&                         xyz.y >= t.botLeftCorner.y &&                         xyz.z <= t.topRightCorner.z &&                         xyz.z >= t.botLeftCorner.z) {```