{"id":2064,"date":"2024-05-10T07:08:59","date_gmt":"2024-05-10T07:08:59","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2064"},"modified":"2024-05-10T07:08:59","modified_gmt":"2024-05-10T07:08:59","slug":"using-range-in-switch-case-in-c-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/using-range-in-switch-case-in-c-c\/","title":{"rendered":"Using Range In Switch Case In C\/C++"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#using-range-in-switch-case-in-c-c\">Using Range In Switch Case In C\/C++<\/a><\/li><li ><a href=\"#syntax\">Syntax <\/a><\/li><li ><a href=\"#complexity-analysis\">Complexity Analysis<\/a><\/li><li ><a href=\"#error-conditions\">Error conditions<\/a><\/li><li ><a href=\"#faq-using-range-in-switch-case-in-c-c\">FAQ- Using Range In Switch Case In C\/C++<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-range-in-switch-case-in-c-c\">Using Range In Switch Case In C\/C++<\/h2>\n\n\n\n<p>This extension allows you to specify a range of consecutive values in a single <code>case<\/code> label, which can be quite convenient for simplifying code when you need to perform the same actions for a range of values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"syntax\">Syntax <\/h2>\n\n\n\n<p>The syntax for using range case is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case low ... high:\n<\/code><\/pre>\n\n\n\n<p>It can be used for a range of ASCII character codes like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case 'A' ... 'Z':\n<\/code><\/pre>\n\n\n\n<p>Note:<\/p>\n\n\n\n<p>You have to write ellipses (&#8230;)<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Correct  -   case 1 ... 5:\n\/\/ Wrong -    case 1...5: <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate\n\/\/ using range in switch case\n#include &lt;stdio.h&gt;\nint main()\n{\n    int arr&#91;] = { 1, 5, 15, 20 };\n \n    for (int i = 0; i &lt; 4; i++) {\n        switch (arr&#91;i]) {\n            \/\/ range 1 to 6\n        case 1 ... 6:\n            printf(\"%d in range 1 to 6\\n\", arr&#91;i]);\n            break;\n            \/\/ range 19 to 20\n        case 19 ... 20:\n            printf(\"%d in range 19 to 20\\n\", arr&#91;i]);\n            break;\n        default:\n            printf(\"%d not in range\\n\", arr&#91;i]);\n            break;\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 in range 1 to 6\n5 in range 1 to 6\n15 not in range\n20 in range 19 to 20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"complexity-analysis\">Complexity Analysis<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Time Complexity: O(n)\n<ul class=\"wp-block-list\">\n<li>Time complexity is a measure of how the execution time of an algorithm or program scales with the input size.<\/li>\n\n\n\n<li>In this case, the algorithm or code snippet has a time complexity of O(n), where &#8216;n&#8217; represents the size of the array &#8216;arr.&#8217;<\/li>\n\n\n\n<li>This means that as the size of the input array &#8216;arr&#8217; increases, the time it takes for the algorithm or code to run will also increase linearly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Auxiliary Space Complexity: O(1)\n<ul class=\"wp-block-list\">\n<li>Auxiliary space complexity refers to the amount of additional memory space used by the algorithm or code, excluding the input data.<\/li>\n\n\n\n<li>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.<\/li>\n\n\n\n<li>In other words, the amount of additional memory used does not depend on the size of the input array &#8216;arr.&#8217;<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"error-conditions\">Error conditions<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>low &gt; high:&nbsp;<\/strong>The compiler gives an error message.<\/li>\n\n\n\n<li><strong>Overlapping case values:&nbsp;<\/strong>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.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-using-range-in-switch-case-in-c-c\">FAQ- Using Range In Switch Case In C\/C++<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695375856396\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to specify a range in the switch case in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. You can use the <code>...<\/code> operator to specify a range of values in a <code>case<\/code> 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.<br \/>For example, if you want to handle uppercase letters &#8216;A&#8217; through &#8216;Z&#8217; in a switch statement.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695375875096\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Can you use a range in a switch statement?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695375887290\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Can we test the range 5 to 7 or 8 to 10 using a switch statement in C programming?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>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 <code>case<\/code> label, which can simplify code and make it more concise.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>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: It can be &#8230; <a title=\"Using Range In Switch Case In C\/C++\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/using-range-in-switch-case-in-c-c\/\" aria-label=\"More on Using Range In Switch Case In C\/C++\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2067,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[363],"class_list":["post-2064","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-using-range-in-switch-case-in-c-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2064"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2064\/revisions"}],"predecessor-version":[{"id":10624,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2064\/revisions\/10624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2067"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}