• Javascript
  • Python
  • Go

Using Switch Statements for Non-Integer Types in C/C++

Switch statements in C/C++ are powerful tools for controlling the flow of a program's execution. They allow developers to handle multiple ca...

Switch statements in C/C++ are powerful tools for controlling the flow of a program's execution. They allow developers to handle multiple cases of a variable or expression in a concise and efficient manner. However, many developers are not aware that switch statements can also be used for non-integer types. In this article, we will explore how to use switch statements for non-integer types in C/C++.

Before we dive into the details, let's first understand what switch statements are. A switch statement is a control structure that allows the program to execute different blocks of code based on the value of a variable or expression. It works by evaluating the value of the variable or expression and then comparing it to different cases. If a match is found, the corresponding block of code is executed.

Traditionally, switch statements are used with integer types such as int, char, and enum. For example, consider the following code snippet:

```

int day = 5;

switch (day) {

case 1:

printf("Monday");

break;

case 2:

printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thursday");

break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

case 7:

printf("Sunday");

break;

default:

printf("Invalid day");

}

```

In this example, the switch statement evaluates the value of the variable `day` and executes the corresponding case. If the value of `day` is 5, the output will be "Friday". However, switch statements can also be used for non-integer types such as float, double, and even custom types.

To use switch statements with non-integer types, we need to understand two concepts: value ranges and type promotion. Value ranges refer to the set of values that can be compared using a switch statement. For example, for integer types, the value range is the set of all possible integer values. Type promotion refers to the automatic conversion of a value to a larger data type if necessary. For example, an int value can be promoted to a float or double value.

Now, let's see how we can use switch statements with non-integer types. Consider the following code snippet:

```

float temperature = 35.2;

switch ((int)temperature) {

case 30:

printf("Moderate heat");

break;

case 35:

printf("High heat");

break;

case 40:

printf("Extreme heat");

break;

default:

printf("No matching temperature range");

}

```

In this example, we are using type casting to convert the float value of `temperature` to an int value. This allows us to use the switch statement with a value range of 30, 35, and 40. If the value of `temperature` falls within one of these ranges, the corresponding case will be executed. Notice that we are not using a `break` statement after each case. This is because the value of `temperature` will not exactly match any of the cases, but it falls within the range of one of the cases. Therefore, the code execution will fall through to the next case until a `break` statement is encountered.

Another way to use switch statements with non-integer types is by using type promotion. Consider the following code snippet:

```

double price = 49.99;

switch (price) {

case 49.99:

printf("Product A");

break;

case 59.99:

printf("Product B");

break;

case 69.99:

printf("Product C");

break;

default:

printf("Product not found");

}

```

In this example, the double value of `price` is automatically promoted to a larger data type of double. This allows us to use the exact value of `price` in our switch statement. If the value of `price` is 49.99, the output will be "Product A". However, if the value of `price` is 49.98, the output will be "Product not found" as there is no case for that specific value.

Finally, switch statements can also be used with custom types. To do this, we need to define the equality operator (==) for our custom type, and the value range will be all possible values of that type. Consider the following code snippet:

```

struct Employee {

int id;

char name[50];

};

Employee emp1 = { 1234, "John" };

switch (emp1) {

case { 1234, "John" }:

printf("Employee found");

break;

default:

printf("Employee not found");

}

``

Related Articles

Using pthread.h on Windows Build

Title: Using pthread.h on Windows Build Pthreads, which stands for POSIX Threads, is a standard thread API that is commonly used in Unix-bas...

When to use bit fields

When working with data in programming, there are often situations where we need to store a set of related binary flags or options in a singl...

Top C/C++ Network Libraries

When it comes to building robust and efficient network applications, having a reliable and powerful network library is crucial. And in the w...