Using Range In Switch Case In C/C++ https://www.skillvertex.com/blog Fri, 10 May 2024 07:08:59 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Using Range In Switch Case In C/C++ https://www.skillvertex.com/blog 32 32 Using Range In Switch Case In C/C++ https://www.skillvertex.com/blog/using-range-in-switch-case-in-c-c/ https://www.skillvertex.com/blog/using-range-in-switch-case-in-c-c/#respond Fri, 10 May 2024 07:08:59 +0000 https://www.skillvertex.com/blog/?p=2064 Read more]]>

Table of Contents

Using Range In Switch Case In C/C++

This extension allows you to specify a range of consecutive values in a single case label, which can be quite convenient for simplifying code when you need to perform the same actions for a range of values.

Syntax

The syntax for using range case is:

case low ... high:

It can be used for a range of ASCII character codes like this:

case 'A' ... 'Z':

Note:

You have to write ellipses (…)

Example:

// Correct  -   case 1 ... 5:
// Wrong -    case 1...5: 
// C program to illustrate
// using range in switch case
#include <stdio.h>
int main()
{
    int arr[] = { 1, 5, 15, 20 };
 
    for (int i = 0; i < 4; i++) {
        switch (arr[i]) {
            // range 1 to 6
        case 1 ... 6:
            printf("%d in range 1 to 6\n", arr[i]);
            break;
            // range 19 to 20
        case 19 ... 20:
            printf("%d in range 19 to 20\n", arr[i]);
            break;
        default:
            printf("%d not in range\n", arr[i]);
            break;
        }
    }
    return 0;
}

Output

1 in range 1 to 6
5 in range 1 to 6
15 not in range
20 in range 19 to 20

Complexity Analysis

  1. Time Complexity: O(n)
    • Time complexity is a measure of how the execution time of an algorithm or program scales with the input size.
    • In this case, the algorithm or code snippet has a time complexity of O(n), where ‘n’ represents the size of the array ‘arr.’
    • This means that as the size of the input array ‘arr’ increases, the time it takes for the algorithm or code to run will also increase linearly.
  2. Auxiliary Space Complexity: O(1)
    • Auxiliary space complexity refers to the amount of additional memory space used by the algorithm or code, excluding the input data.
    • Auxiliary space complexity of O(1) indicates that the algorithm or code uses a constant amount of extra memory space regardless of the input size.
    • In other words, the amount of additional memory used does not depend on the size of the input array ‘arr.’

Error conditions

  1. low > high: The compiler gives an error message.
  2. Overlapping case values: If the value of a case label is within a case range that has already been used in the switch statement, the compiler gives an error message.

FAQ- Using Range In Switch Case In C/C++

Q1. How to specify a range in the switch case in C?

Ans. You can use the ... operator to specify a range of values in a case label. This is particularly useful when you want to handle a range of values in a switch statement without writing separate cases for each individual value.
For example, if you want to handle uppercase letters ‘A’ through ‘Z’ in a switch statement.

Q2. Can you use a range in a switch statement?

Ans. In C and C++, you can freely intermix case ranges and case labels within a switch statement. This allows you to handle individual values as well as ranges of values in the same switch statement, providing flexibility and readability in your code.

Q3. Can we test the range 5 to 7 or 8 to 10 using a switch statement in C programming?

Ans. You can use a range of values with a switch-case statement in C or C++ when using a compiler that supports this feature, such as the GNU C Compiler (GCC) extension. This allows you to specify a range of consecutive values in a single case label, which can simplify code and make it more concise.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/using-range-in-switch-case-in-c-c/feed/ 0