{"id":5598,"date":"2023-03-27T10:34:54","date_gmt":"2023-03-27T10:34:54","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=5598"},"modified":"2023-03-27T10:34:59","modified_gmt":"2023-03-27T10:34:59","slug":"switch-case-statement-in-cpp","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/switch-case-statement-in-cpp\/","title":{"rendered":"Switch case statement in C++"},"content":{"rendered":"\n<p>In this tutorial, we are going to explore the switch case statement in C++. A switch case statement is often used in our C++ programs when we are having many cases(blocks), and we need to execute some particular block, which matches the given expression.<\/p>\n\n\n\n<p>For example, if you were to create a calculator, you would take operands, and an operator, for determining which operation needs to be done. So, we have multiple operators, so we write multiple cases, and according to the operator entered, the matching case will be executed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Switch case statement in CPP<\/h2>\n\n\n\n<p>Do not worry if you do not get the broad picture, since we are going to have a look at the syntax of switch case in C++, and we will also go through an example, to understand the use of switch case in our C++ programs.<\/p>\n\n\n\n<p>Here is the syntax for the switch case statement &#8211;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">#include &lt;iostream&gt;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">int main()<br>{<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; switch(expression)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; case expr1 : \/\/ some code here. <br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; case expr2 : \/\/ some code here. <br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; case expr3 : \/\/ some code here.<br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; case expr4 : \/\/ some code here. &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; default: \/\/ some code here. <br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; }<br>return 0;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">}<\/p>\n\n\n\n<p>As you can see in the syntax, we have some of the usual things from the syntax, and then we have the switch keyword, after which, we are going to have some expression, and within the block, we have different cases, where we are going to write some code for some particular case.<\/p>\n\n\n\n<p>For example, let&#8217;s say that we are creating a calculator, and we are getting the operator and 2 operands as user input. In this case, we are going to do the operation on the basis of what operator is there. So, for all the arithmetic operators, we will have the cases, and we will watch for what operator matches, and we will execute that case.<\/p>\n\n\n\n<p>Notice the break keyword at the end of every case? Well, this is just to get out after executing that particular case. In case if break keyword is not there, it will move to the next case executing it, even if that case doesn&#8217;t qualify. It would keep doing this, till the cases are finished, or it finds a break keyword. <\/p>\n\n\n\n<p>Also, we have the default at the last(it is not compulsory to keep it in the last). This default executes if there was no matching case for a given expression.<\/p>\n\n\n\n<p>So, now, let&#8217;s turn to an example, to understand the switch case statement in a better way, and to see it in action.<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">#include &lt;iostream&gt;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">int main()<br>{<br>&nbsp; &nbsp; int var1, var2;<br>&nbsp; &nbsp; char operation;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; std::cout &lt;&lt; &#8220;Please enter the Operator : &#8220;;<br>&nbsp; &nbsp; std::cin &gt;&gt; operation;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; std::cout &lt;&lt; &#8220;Please enter first number : &#8220;;<br>&nbsp; &nbsp; std::cin &gt;&gt; var1;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">&nbsp; &nbsp; std::cout &lt;&lt; &#8220;Please enter second number : &#8220;;<br>&nbsp; &nbsp; std::cin &gt;&gt; var2;<\/p>\n\n\n\n<p class=\"has-ast-global-color-8-color has-black-background-color has-text-color has-background\">\u00a0 \u00a0 switch(operation)<br>\u00a0 \u00a0 {<br>\u00a0 \u00a0 \u00a0 case &#8216;+&#8217; : std::cout &lt;&lt; &#8220;Performing addition:&#8221;;<br>\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt; var1 + var2 &lt;&lt; std::endl;<br>\u00a0 \u00a0 \u00a0 \u00a0 break;<br>\u00a0 \u00a0 \u00a0 case &#8216;-&#8216; : std::cout &lt;&lt; &#8220;Performing subtraction:&#8221;;<br>\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt; var1 &#8211; var2 &lt;&lt; std::endl;<br>\u00a0 \u00a0 \u00a0 \u00a0 break;<br>\u00a0 \u00a0 \u00a0 case &#8216;*&#8217; : std::cout &lt;&lt; &#8220;Performing multiplication:&#8221;;<br>\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt; var1 * var2 &lt;&lt; std::endl;<br>\u00a0 \u00a0 \u00a0 \u00a0 break;<br>\u00a0 \u00a0 \u00a0 case &#8216;\/&#8217; : std::cout &lt;&lt; &#8220;Performing division:&#8221;;<br>\u00a0 \u00a0 \u00a0 \u00a0 std::cout &lt;&lt; var1 \/ var2 &lt;&lt; std::endl;<br>\u00a0 \u00a0 \u00a0 \u00a0 break;<br>\u00a0 \u00a0 \u00a0 default: std::cout &lt;&lt; &#8220;This input was not expected&#8230;&#8221;;<br>\u00a0 \u00a0 \u00a0 \u00a0 break;<br>\u00a0 \u00a0 }<br>return 0;<br>}<\/p>\n\n\n\n<p>As you can see from the above program, we are taking the operator, and two operands as user input, and then we are having the switch block. Here, we are trying to match the operator, and accordingly, we will execute the matching case for the operation. <br>Here is the output &#8211;<\/p>\n\n\n\n<p>Please enter the Operator : *<br>Please enter first number: 23<br>Please enter the second number: 5<br>Performing multiplication:115<\/p>\n\n\n\n<p>As you can see, we had given the operator symbol for multiplication, and it performed multiplication as required. So, you can use the concept of switch case statements in a number of use cases, similar to this. It is usually used in situations when we are having some cases, and we need to execute some particular case that matches the given expression.<\/p>\n\n\n\n<p>Remember that if no case matches, the default will be executed, and do not forget to write the break keyword there, since it would help you to get out of the switch block, after executing the relevant case. Missing break keywords would result in a fall-through.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQs related to switching case statements in C++<\/h4>\n\n\n\n<div class=\"wp-block-rank-math-faq-block\"><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: Is switching a keyword in C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> Yes, the switch is a keyword in C++.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: why there is a break in the switch case statement?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> the break keyword helps get out of the switch block, after executing the relevant case. If it is not present, it will result in a fall-through. It simply means that it would execute the cases after that as well, even if they are not relevant.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What is the default in the switch case?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> If no case matches, then the default case will execute.<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to explore the switch case statement in C++. A switch case statement is often used in our C++ programs when we are having many cases(blocks), and we need to execute some particular block, which matches the given expression. For example, if you were to create a calculator, you would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[40,136,174],"class_list":{"0":"post-5598","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c","7":"tag-c","8":"tag-cpp","9":"tag-switch-case-statement-in-c"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=5598"}],"version-history":[{"count":1,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5598\/revisions"}],"predecessor-version":[{"id":5599,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5598\/revisions\/5599"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=5598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=5598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=5598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}