{"id":182,"date":"2020-08-30T12:58:35","date_gmt":"2020-08-30T12:58:35","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=182"},"modified":"2020-08-30T12:58:35","modified_gmt":"2020-08-30T12:58:35","slug":"c-conditional-statements","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-conditional-statements\/","title":{"rendered":"C++ Conditional Statements"},"content":{"rendered":"<p>In this lesson, we would learn how to use use conditional statement in C++. Conditional statements allow you to alter the direction of the program flow based n certain conditions.<\/p>\n<p>We&#8217;l cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">if statement<\/a><\/li>\n<li><a href=\"#t2\">if..else statement<\/a><\/li>\n<li><a href=\"#t3\">nested if statements<\/a><\/li>\n<li><a href=\"#t4\">switch statement<\/a><\/li>\n<li><a href=\"#t5\">nested switch statement<\/a><\/li>\n<li><a href=\"#t6\">The Conditional Operator(?:)<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. if statement<\/strong><\/h4>\n<p>This consists of a single expression followed by one or more statements. If the expression evaluates to true, then the statement body is executed. Otherwise, the statement body is skipped. The statement body is enclosed in curly braces.<\/p>\n<p>An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #888888;\">\/\/ local variable :<\/span>\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>;\r\n \r\n   <span style=\"color: #888888;\">\/\/ check if the expression is true<\/span>\r\n   <span style=\"color: #008800; font-weight: bold;\">if<\/span>( x <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">100<\/span> ) {\r\n      <span style=\"color: #888888;\">\/\/ the statement body<\/span>\r\n      cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"x is less than 20 \\n\"<\/span>;\r\n   }\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"value of x is : \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> x;\r\n \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>Output<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">a is less than 20;\r\nvalue of a is : 10\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. if..else statement<\/strong><\/h4>\n<p>This is an if statement followed by an else part. The else part is executed if the\u00a0 conditional expression is false.<\/p>\n<p>An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"How old are you? \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> age;\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">if<\/span>(age <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">16<\/span>) {\r\n\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"You are an adult!\"<\/span>;\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"You are not yet an adult! \"<\/span>;\r\n\t}\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Because you are \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> age <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\" years old\"<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. nested if statements<\/strong><\/h4>\n<p>In this case, we use on if..else statement inside another on. It will be best understood with an example. The previous example is modified to include a nested if statement<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"How old are you? \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> age;\r\n\r\n\t<span style=\"color: #008800; font-weight: bold;\">if<\/span>(age <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">16<\/span>) {\r\n\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"You are an adult! \/n\"<\/span>;\r\n\t\t<span style=\"color: #888888;\">\/\/ Nested if statement<\/span>\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span>(age <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">18<\/span>){\r\n\t\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"But you cannot enlist!\"<\/span>\r\n\t\t}\r\n\t}\r\n\t<span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"You are not yet an adult! \"<\/span>;\r\n\t}\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Because you are \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> age <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\" years\"<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. switch statement<\/strong><\/h4>\n<p>The switch statement\u00a0 enables you to test a particular variable\u00a0 against a list of values. When the variable matches any of the items in the list, a particular statement or statements are executed.<\/p>\n<p>The syntax for the switch statement is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">switch<\/span>(expression) {\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> expression_1  :\r\n      statements;\r\n      <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> expression_2  :\r\n      statements;\r\n      <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n  ...\r\n  ...\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> expression_n  :\r\n      statements;\r\n      <span style=\"color: #008800; font-weight: bold;\">break<\/span>;  \r\n   <span style=\"color: #008800; font-weight: bold;\">default<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #888888;\">\/\/Optional<\/span>\r\n      statements;\r\n}\r\n<\/pre>\n<p>A typical example would be to display a gradesheet where students are assigned a grade based on their score in an exam.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #333399; font-weight: bold;\">char<\/span> grade;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Please enter your grade: \"<\/span>;\r\n   <span style=\"color: #888888;\">\/\/Read the grade value from user input<\/span>\r\n   cin <span style=\"color: #333333;\">&gt;&gt;<\/span> grade;\r\n   <span style=\"color: #008800; font-weight: bold;\">switch<\/span>(grade) {\r\n      <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'A'<\/span> :\r\n         cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Excellent!\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n         <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n      <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'B'<\/span> :\r\n    \t  cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Very Good!\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n      <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'C'<\/span> :\r\n         cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Good\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n         <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n      <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'D'<\/span> :\r\n         cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Pass\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n         <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n      <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'F'<\/span> :\r\n         cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Fail\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n         <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n      <span style=\"color: #008800; font-weight: bold;\">default<\/span> <span style=\"color: #333333;\">:<\/span>\r\n         cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Invalid grade\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n   }\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Your grade: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> grade <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. nested switch statement<\/strong><\/h4>\n<p>You can also have a switch statement nested within another switch statement. This works the same way as nested if statement but in this case, it&#8217;s switch statement.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">switch<\/span>(char1) {\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'C'<\/span>: \r\n\t  cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"This C is part of outer switch\"<\/span>;\r\n\t  <span style=\"color: #008800; font-weight: bold;\">switch<\/span>(char2) {\r\n\t\t <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'C'<\/span>:\r\n\t\t\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"This C is part of inner switch\"<\/span>;\r\n\t\t\t<span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n\t\t <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'D'<\/span>: <span style=\"color: #888888;\">\/\/ ...<\/span>\r\n\t  }\r\n\t  <span style=\"color: #008800; font-weight: bold;\">break<\/span>;\r\n   <span style=\"color: #008800; font-weight: bold;\">case<\/span> <span style=\"color: #0044dd;\">'B'<\/span>: <span style=\"color: #888888;\">\/\/ ...<\/span>\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. The Conditional Operator (? \ud83d\ude42<\/strong><\/h4>\n<p>We touched on the conditional operator previously in the C++ Operators lesson. This operator is also called <strong><em>&#8216;ternary operator<\/em><\/strong>&#8216;. It takes three operands like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Expr1 <span style=\"color: #333333;\">?<\/span> Expr2 <span style=\"color: #333333;\">:<\/span> Expr3;\r\n<\/pre>\n<p><strong>How it works:<\/strong><\/p>\n<ul>\n<li>the first expression is evaluation and results in a boolean(true or false)<\/li>\n<li>if true, then evaluation the second expression (Expr2)<\/li>\n<li>if false, then evaluate the third expression (Expr3)<\/li>\n<\/ul>\n<p>The result of the conditional operation is the result of whichever expression is evaluated between Expr2 and Expr3<\/p>\n<p>An example is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> x <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> y <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>;\r\n   <span style=\"color: #333399; font-weight: bold;\">int<\/span> z;\r\n\r\n   z <span style=\"color: #333333;\">=<\/span> (x <span style=\"color: #333333;\">&lt;<\/span> y) <span style=\"color: #333333;\">?<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span> <span style=\"color: #333333;\">:<\/span> <span style=\"color: #0000dd; font-weight: bold;\">40<\/span>;\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"value of x: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> z <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n \r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">value of z: 10\r\n<\/pre>\n<p>Here, z is assigned the value 10. This is because when the expression (x &lt; y) is evaluated, the result is true. So the conditional operation returns 10 which is assigned to z<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would learn how to use use conditional statement in C++. Conditional statements allow you to alter the direction of the program &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[30,29],"class_list":["post-182","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-conditional-statements","tag-if-statement"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/182\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}