

{"id":146391,"date":"2025-08-01T12:39:20","date_gmt":"2025-08-01T07:09:20","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=146391"},"modified":"2025-08-01T12:39:20","modified_gmt":"2025-08-01T07:09:20","slug":"switch-statement-in-cpp","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/","title":{"rendered":"Switch Statement in C++"},"content":{"rendered":"<h3>Program 1<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ Swtich Case in cpp\r\n\/*\r\n  switch(value)\r\n  {\r\n   case one: statement1 \r\n   case two:statement2\r\n   case\r\n   default: \r\n  }\r\n*\/\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\nint getValue();\r\nint main()\r\n{\r\n    system(\"cls\");\r\n    int n;\r\n    cout&lt;&lt;\"Enter a number\";\r\n    cin&gt;&gt;n;\r\n    switch(n)  \/\/n=2\r\n    {\r\n        case 1: cout&lt;&lt;\"One\";break;\r\n        case 2: cout&lt;&lt;\"Two\"; break;  \r\n        case 3: cout&lt;&lt;\"Three\";break;\r\n        case 4: cout&lt;&lt;\"Four\";break;\r\n        case 5: cout&lt;&lt;\"Five\";break;\r\n        default:cout&lt;&lt;\"Invalid Number\";break;\r\n    }\r\n    cout&lt;&lt;\"\\n Bye Bye\";\r\n    return 0;\r\n}<\/pre>\n<h3>Program 2<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ Vowel \r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\nint getValue();\r\nint main()\r\n{\r\n     char ch;\r\n     system(\"cls\");\r\n     cout&lt;&lt;\"Enter a character\";\r\n     cin&gt;&gt;ch;   \r\n     switch(ch)\r\n     {\r\n        case 'a':\r\n        case 'e':\r\n        case 'i':\r\n        case 'o':\r\n        case 'u':\r\n        case 'A':\r\n        case 'E':\r\n        case 'I':\r\n        case 'O':\r\n        case 'U':cout&lt;&lt;\"Vowel\";break;\r\n\r\n        default:cout&lt;&lt;\"Not a Vowel\";\r\n     }\r\n     return 0;\r\n}<\/pre>\n<h3>Program 3<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;iostream&gt;\r\nusing namespace std;\r\nint getValue();\r\nint main()\r\n{\r\n    system(\"cls\");\r\n     switch(int n=getValue())\r\n     {\r\n        case 1:cout&lt;&lt;\"One\";break;\r\n        case 2:cout&lt;&lt;\"Two\";break;\r\n        case 3:cout&lt;&lt;\"Three\";break;\r\n        case 4:cout&lt;&lt;\"Four\";break;\r\n     }\r\n}\r\nint getValue()\r\n{\r\n    int x;\r\n    cout&lt;&lt;\"Enter a number\";\r\n    cin&gt;&gt;x;\r\n    return x;\r\n}<\/pre>\n<h3>Program 4<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ Simple Calculator Project\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\nint main()\r\n{\r\n    int n1,n2;\r\n    char opr;\r\n    system(\"cls\");\r\n    cout&lt;&lt;\"-----------Simple Calculator---------------\\n\\n\";\r\n    cout&lt;&lt;\"\\n First Number: \";\r\n    cin&gt;&gt;n1;\r\n    cout&lt;&lt;\"+ , - , * , \/ ,% :  \";\r\n    cin&gt;&gt;opr;\r\n    cout&lt;&lt;\"\\nSecond Number: \";\r\n    cin&gt;&gt;n2;\r\n    switch(opr)\r\n    {\r\n        case '+': cout&lt;&lt; n1+n2;break;\r\n        case '-': cout&lt;&lt; n1-n2;break;\r\n        case '*': cout&lt;&lt; n1*n2;break;\r\n        case '\/': \r\n         {\r\n               if(n2!=0)\r\n                cout&lt;&lt;n1\/n2;\r\n               else\r\n               cout&lt;&lt;\"Can't divide by zero\";\r\n               break;\r\n         } \r\n       case '%': \r\n         {\r\n               if(n2!=0)\r\n                cout&lt;&lt;n1%n2;\r\n               else\r\n               cout&lt;&lt;\"Can't divide by zero\";\r\n               break;\r\n         }   \r\n        default: cout&lt;&lt;\"Invalid operator \"; \r\n    }\r\n    \r\n    return 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Program 1 \/\/ Swtich Case in cpp \/* switch(value) { case one: statement1 case two:statement2 case default: } *\/ #include&lt;iostream&gt; using namespace std; int getValue(); int main() { system(&#8220;cls&#8221;); int n; cout&lt;&lt;&#8220;Enter a number&#8221;;&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20172],"tags":[34257,28585,2263,29218,35003,34163,35002,34167,33776,28333],"class_list":["post-146391","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c-simple-calculator","tag-c-tutorial","tag-c","tag-c-program","tag-c-program-on-switch-case-statement","tag-c-simple-calculator-project","tag-c-switch-case-statement","tag-simple-calculator-in-c","tag-switch-case-statement-in-c","tag-switch-statement-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Switch Statement in C++ - DataFlair<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Switch Statement in C++ - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Program 1 \/\/ Swtich Case in cpp \/* switch(value) { case one: statement1 case two:statement2 case default: } *\/ #include&lt;iostream&gt; using namespace std; int getValue(); int main() { system(&quot;cls&quot;); int n; cout&lt;&lt;&quot;Enter a number&quot;;&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-01T07:09:20+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Switch Statement in C++ - DataFlair","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Switch Statement in C++ - DataFlair","og_description":"Program 1 \/\/ Swtich Case in cpp \/* switch(value) { case one: statement1 case two:statement2 case default: } *\/ #include&lt;iostream&gt; using namespace std; int getValue(); int main() { system(\"cls\"); int n; cout&lt;&lt;\"Enter a number\";&#46;&#46;&#46;","og_url":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2025-08-01T07:09:20+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Switch Statement in C++","datePublished":"2025-08-01T07:09:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/"},"wordCount":8,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["c simple calculator","c tutorial","C++","c++ program","c++ program on switch case statement","c++ simple calculator project","c++ switch case statement","simple calculator in c++","switch case statement in c","switch statement in c"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/","url":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/","name":"Switch Statement in C++ - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2025-08-01T07:09:20+00:00","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/switch-statement-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C++ Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/cpp\/"},{"@type":"ListItem","position":3,"name":"Switch Statement in C++"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/146391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=146391"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/146391\/revisions"}],"predecessor-version":[{"id":146439,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/146391\/revisions\/146439"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=146391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=146391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=146391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}