In C, it is not uncommon to have to check if a variable is included in a given interval. For example, between 0 and 100 for a percentage.

There are several solutions, we can obviously nest `if`

to obtain a code of the type:

```
if (p>=0) {
if (p<=100) {
// Valid percentage
// ...
}
}
```

Suppose we also want to execute a block of instructions when the percentage is not valid.
You will quickly realize that this solution does not allow to insert an `else`

properly.

Beginning developers would be tempted to write the following code:

```
if ( 0 <= p <= 100 ) {
// Valid percentage
// ...
}
```

This solution compiles but will not have the expected behavior. Indeed, to analyze
this code, you have to refer to the operator precedence table.
The operator `<=`

has associativity from left to right, so it is like writing :

`if ( (0 <= p) <= 100 ) { ... }`

The test `(0 <= p)`

can only return 0 or 1. In both cases, it will always be
less than 100. The test will always be true, so you might as well delete it, it won't change anything.

The right solution to test an interval is to use a logical operator to combine the two tests:

```
if ( p>=0 && p<=100 ) {
// Valid percentage
// ...
}
```

This time, this solution will work. The test will be true if `p`

is greater than 0 **and**
`p`

is less than 100.

There is another solution which is related to the previous one by De Morgan's law. We will demonstrate this. If you are not familiar with Boolean logic, this should not prevent you from you from understanding the final solution.

In the test `if ( p>=0 && p<=100 )`

, we will set the following boolean variables:

**A**is true if`p>=0`

**B**is false if`p<=100`

Our condition becomes `if ( A && B )`

, which is represented by the logical equation:

$$ A.B $$

By applying De Morgan's law, we can now write :

$$ A.B = \overline { \overline{A} + \overline{B}} $$

If we replace back A and B by `p>=0`

and `p<=100`

respectively, we can also write :

$$ \overline{A} = \overline{(p>=0)} = p<0 $$ $$ \overline{B} = \overline{(p<=100)} = p>100 $$

This gives us the following alternative code:

```
if (!( p<0 || p>100 )) {
//Valid percentage
// ...
}
```

Do not mix the two solutions with a writing like

All integers are greater than 0**OR** less than 100.

This test is always true.

`if ( p>=0 || p<=100 )`

.All integers are greater than 0

This test is always true.

Write a program that asks the user to enter a grade. If the grade
is between 0 and 20, the program displays `Grade recorded`

, otherwise it displays
`Non valid grade`

.

```
float grade;
// Ask for the grade
// COMPLETE HERE
// Check if the grade is valid [0;20]
// COMPLETE HERE
```

Here is the expected display:

```
Enter your grade: 23
Non valid grade
```

Write a program that displays the notation associated with a note entered by the user by testing each interval.

Interval | Message |
---|---|

[0 ; 10[ | rejected |

[10 ; 12[ | Passing |

[12 ; 14[ | With honors |

[14 ; 16[ | High honors |

[16 ; 20] | Highest honors |

Here is the expected display:

```
Enter your grade: 13.8
With honors
```

**What does the following code display?**

```
if ( 0<x<10 )
printf ("Crac");
else
printf ("Boum");
```

Check
Bravo! The test is useless because it will always be true. Operators precedence =>

`(0<note)<100`

.
Try again...
**How to test if an integer a is between 12 and 52 inclusive?**

Check
Bravo! It requires that **AND** less than or equal to 52.
Try again...

`a`

be greater than or equal to 12 **What does the following code display?**

```
if ( x<0 && x>100 )
printf ("Crac");
else
printf ("Boum");
```

Check
Bravo! So try to find a number smaller than 0 **AND** greater than 100. Good luck!
Try again...

**How to test if an integer a isn't in the interval [12 ; 52] (in other words if a is 12, the test is false) ?**

Check
Bravo! The variable **OR** strictly greater than 52.
Try again...

`a`

must be strictly less than 12 **What does the following test do?**

`if ((x<=7 || x>=12) == 0)`

Check
Bravo! By applying De Morgan's law, we can show this is equivalent to

`if (x>7 && x<12)`

.
Try again...
- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/22/2022