Range Check

Range checking involves determining if a given number lies within a specified range. This is useful for validating user input or constraining values to an expected domain.

Java example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public static boolean isInRange(int num, int lower, int upper) {
  if (num >= lower && num <= upper) {
    return true;
  }
  return false;
}

int num = 5;
int lower = 1;
int upper = 10;

if(isInRange(num, lower, upper)) {
  System.out.println(num + " is in range"); 
} else {
  System.out.println(num + " is not in range");
}

C++ example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
bool isInRange(int num, int lower, int upper) {
  return (num >= lower && num <= upper); 
}

int num = 5;
int lower = 1; 
int upper = 10;

if (isInRange(num, lower, upper)) {
  cout << num << " is in range" << endl;
} else {
  cout << num << " is not in range" << endl;  
}

Python example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def is_in_range(num, lower, upper):
  if num >= lower and num <= upper:
    return True
  else:
    return False

num = 5 
lower = 1
upper = 10

if is_in_range(num, lower, upper):
  print(str(num) + " is in range")
else:
  print(str(num) + " is not in range")  

The key steps are:

  1. Check if the number is greater than or equal to the lower bound

  2. Check if the number is less than or equal to the upper bound

  3. Return true if both conditions are met, false otherwise

This allows verifying that a number is within a given inclusive range.

In programming, range checking involves verifying that a given value falls within a specified range. This is crucial to ensure that the program operates correctly and securely. Range checks are often performed on array indices, variables, and input data. Failing to perform range checks can lead to issues like buffer overflows or unexpected behavior.

For instance, if you have an array of size N, the valid indices range from 0 to N-1. A range check would confirm that an index is within this limit before using it to access array elements.

Example Code

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class RangeCheckExample {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int index = 6; // An example index that is out of range

        if (index >= 0 && index < array.length) {
            System.out.println("Value at index " + index + ": " + array[index]);
        } else {
            System.out.println("Index is out of range.");
        }
    }
}

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> vec = {1, 2, 3, 4, 5};
    int index = 6; // An example index that is out of range

    if (index >= 0 && index < vec.size()) {
        cout << "Value at index " << index << ": " << vec[index] << endl;
    } else {
        cout << "Index is out of range." << endl;
    }
    return 0;
}

Python

1
2
3
4
5
6
7
array = [1, 2, 3, 4, 5]
index = 6  # An example index that is out of range

if 0 <= index < len(array):
    print(f"Value at index {index}: {array[index]}")
else:
    print("Index is out of range.")

In these code examples, we perform a simple range check on the array index. The range check validates that the index is within the bounds of the array before accessing its elements. If the check fails, a message is printed to indicate that the index is out of range.