### Conditions

Sometimes, we may need to run some instructions depending on a **condition**, for example: imagine that our calculator program can divide and we ask for the two numbers to make the division, the second number **CAN NOT** be `0`

, because it makes a mathematical exception, so we are going to divide the two numbers and show the result only **if** the second number is different than `0`

.

`if`

statement

We can do that validation in our calculator program **using a condition**, and we construct a condition using the `if`

statement, it looks like this:

Now, to write the conditions we use **logical expressions**. In `c`

we use these operators to code logical expressions:

#### Equality Operators

Symbol | Description |
---|---|

`==` |
Equal to |

`!=` |
Different than |

When a condition is evaluated it returns `0`

considered as **false** or `1`

as **true**. The condition for our calculator program would be:

#### Relational Operators

Symbol | Description |
---|---|

`<` |
Less than |

`>` |
Greater than |

`<=` |
Less than or equal to |

`>=` |
Greater than or equal to |

These operators allow us to compare values and make decisions if one value is greater or less than another in just one expression. Maybe the operator `<=`

and `>=`

can result confusing, you can think about that the value that you are comparing to can be included or excluded in the range of comparison.

Let’s see the difference with an example:

#### Logical Operators

Symbol | Description |
---|---|

`!` |
Logical negation |

`&&` |
Logical and |

`||` |
Logical or |

Sometimes you need to take in account two or more conditions to make a decision, here are the logical operators to help you. Imagine that need to make sure that a value is inside two numbers, suppose that you need to be greater than 0 **and** less than 10:

For the logical *or* example you can think about that you allow only two numbers, 0 or 10:

There is only one logical operator left to explain and is the logical *negation*, it lets you to deny an expression, example, a value can not be equal to 30:

#### What a lesson!

#### We know this could be difficult at the beginning, especially when you are using the logical operators `&&`

and `||`

, we recommend you that if your code doesn’t behave as you expect take a look at these tables and write in paper the possible values to check what is going on:

`&&`

behavior and possible values

Condition A | LO | Condition B | Result |
---|---|---|---|

`false` |
`&&` |
`false` |
`false` |

`false` |
`&&` |
`true` |
`false` |

`true` |
`&&` |
`false` |
`false` |

`true` |
`&&` |
`true` |
`true` |

## Only is true when all the conditions are

`true`

`||`

behavior and possible values

Condition A | LO | Condition B | Result |
---|---|---|---|

`false` |
`||` |
`false` |
`false` |

`false` |
`||` |
`true` |
`true` |

`true` |
`||` |
`false` |
`true` |

`true` |
`||` |
`true` |
`true` |

## Only is false when all the conditions are

`false`