{"id":5700,"date":"2023-03-29T11:29:07","date_gmt":"2023-03-29T11:29:07","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=5700"},"modified":"2023-03-29T12:28:54","modified_gmt":"2023-03-29T12:28:54","slug":"logical-operators-in-cpp","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/logical-operators-in-cpp\/","title":{"rendered":"Logical operators in C++"},"content":{"rendered":"\n<p>In this tutorial, we are going to explore the concept of logical <a href=\"https:\/\/gyanipandit.com\/programming\/operators-in-cpp\/\">operators in C++<\/a>. The logical operators are used to check whether an expression is true, or false. Again here, if the expression is true, we get 1, and if the expression is false, we get 0. Let&#8217;s have a look at some of the logical operator symbols that we have here.<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Logical operators in CPP<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Operator<\/th><th>Description<\/th><th>Example<\/th><\/tr><tr><td>&amp;&amp;<\/td><td>This is a logical AND operator. The condition becomes true if all the operands(values) are non-zero.<\/td><td>a&amp;&amp;b<\/td><\/tr><tr><td>||<\/td><td>This is the logical OR operator. This returns true if any operand (value) is non-zero.<\/td><td>a||b<\/td><\/tr><tr><td>!<\/td><td>This is the logical NOT operator. It converts true to false and false to true.<\/td><td>!(5 &gt;= 2)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>As you can see in the above table, we have listed some logical operator symbols, which we can simply use in our programs. Now, let&#8217;s have a look at a simple C++ program, in which, we will try to make use of the above-stated logical operators.<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">#include &lt;iostream&gt;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">int main()<br>{<br>int a = 6, b = 2;<br>std::cout &lt;&lt; &#8220;a is even and less than 10: &#8220;&lt;&lt; ((a %2==0) &amp;&amp; (a&lt;10)) &lt;&lt; std::endl;<br>std::cout &lt;&lt; &#8220;a is even or a is less than 10: &#8220;&lt;&lt; ((a %2==0) || (a&lt;10)) &lt;&lt; std::endl;<br>std::cout &lt;&lt; &#8220;Logical not operator: &#8220;&lt;&lt; (!(a %2==0)) &lt;&lt; std::endl;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">return 0;<br>}<\/p>\n\n\n\n<p>As you can see in the above program, we have tried using different logical operators, like logical AND, logical OR, and logical NOT.<\/p>\n\n\n\n<p>Here is the output of the above program &#8211;<\/p>\n\n\n\n<p>a is even and less than 10: 1<br>a is even or a is less than 10: 1<br>Logical not operator: 0<\/p>\n\n\n\n<p>As you can see, 1 means true here, and 0 means false. You can use them in your programs, as and when required.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Summary<\/h4>\n\n\n\n<p>In this tutorial, we have learned about the logical operators in C++. At times, we might require to use the logical operators in our C++ programs, especially, when we would be dealing with if-else statements, at that time, when we would have multiple conditions, we would need these logical operators.<\/p>\n\n\n\n<p>If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to <a href=\"https:\/\/gyanipandit.com\/programming\/what-is-python\/\">Python<\/a>, <a href=\"https:\/\/gyanipandit.com\/programming\/what-is-machine-learning\/\">Machine learning<\/a>, and Data Science, through which, you can upskill yourself.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">FAQs related to Logical Operators 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: What are logical operators in C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans:<\/strong> Using the logical operators, we can check if the expression is true or false.<\/div><\/div><div class=\"rank-math-faq-item\"><h3 class=\"rank-math-question\">Q: What are the different symbols for logical operators in C++?<\/h3><div class=\"rank-math-answer\"><strong>Ans: <\/strong>Logical operator symbols in C++ include (&amp;&amp;, ||, !)<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we are going to explore the concept of logical operators in C++. The logical operators are used to check whether an expression is true, or false. Again here, if the expression is true, we get 1, and if the expression is false, we get 0. Let&#8217;s have a look at some of [&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,207],"class_list":{"0":"post-5700","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c","7":"tag-c","8":"tag-cpp","9":"tag-logical-operators-in-c"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5700","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=5700"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5700\/revisions"}],"predecessor-version":[{"id":5717,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/5700\/revisions\/5717"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=5700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=5700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=5700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}