{this program will print a message if the number is between 3 and 5}
program u35a;
var number:real;
begin
write('please give me a number to check the range of ');
read(number);

if (3 <= number) and (number <= 5) then begin
write('your number is in the range [3,5]');
writeln;
end;

end.

{this program will print a message if the number is NOT between 3 and 5}
program u35b;
var number:real;
begin
write('please give me a number to check the range of ');
read(number);

if (3 > number) or (number > 5) then begin
write('your number is NOT in the range [3,5]');
writeln;
end;

end.



Unit 3.5 Checking Ranges of Numbers

PROG

first program (u35A.pas) -- see if number is between 3 and 5

second program (u35B.pas) -- see if number is not between 3 and 5

PED

To learn how to write code to check if a number is in a range

CONCEPT

to check if number is in range [x,y] where x,y variables or constants

if (x<=number) and (number <= y) then begin
statements to execute when number is in range
end;

to check if number is NOT in range [x,y] where x,y variables or constants

if (x>number) or (number < y) then begin
statements to execute when number is not in range
end;



Unit 3.5 Checking ranges

A very frequent operation in programming is to check whether a number is in a given range. For example, in computing taxes, certain rates or deductions only apply to those people whose incomes are between two values specified in the law.
We also learn how to tell if a number is NOT in a given range.

We will use the "and" and the "or" we learned earlier to do this.

The notation [x,y] refers to the range of all numbers between x and y. More formally, this is all numbers that are both greater than or equal to x and also less than or equal to y. We check this with the condition (x<=number) and (number<=y).

A number is not in the range [x,y] if the number is less than x. Or, it might be greater than y in which case it would be too large to be in the range. We can check this with the condition (x>number) or (number<y).

The first program checks if a number is between 3 and 5. This is the range [3,5]. Note the conditon on line 8. Note we put 3 where we had x in the template and 5 where we had 5 in the template.

The second program checks if a number is NOT between 3 and 5. We use the second template, substituting 3 for x and 5 for y.

Note: sometimes, we talk about the non-inclusive range (3,5). Note the use of parentheses instead of brackets. That means that 3 and 5 are not in the range. If we have this situation, we would use

(x<number) and (number<y)

A number would be outside the open-ended range if (x>=number) or (number <= y).