{"id":5395,"date":"2018-02-23T17:17:32","date_gmt":"2018-02-23T17:17:32","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-statements\/"},"modified":"2023-06-26T11:55:53","modified_gmt":"2023-06-26T11:55:53","slug":"c-core-guidelines-rules-for-statements","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-for-statements\/","title":{"rendered":"C++ Core Guidelines: Rules for Statements"},"content":{"rendered":"<p>Before I continue with the roughly 15 rules for statements, let me finish the two rules for expressions. Both rules help you to protect your program from undefined behavior.&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5392\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/UnderConstruction.png\" alt=\"UnderConstruction\" width=\"500\" height=\"449\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/UnderConstruction.png 801w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/UnderConstruction-300x270.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/UnderConstruction-768x690.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Here are the two remaining rules for expressions.<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-construct\" style=\"color: #268bd2; text-decoration: underline;\">ES.64: Use the&nbsp;<code class=\"highlighter-rouge no-highlight\" style=\"font-family: 'Roboto Mono', monospace; padding: 0.2em; font-size: 18px; background-color: #f9f9f9;\">T{e}<\/code>notation for construction<\/a><\/h3>\n<p>The reason for using <span style=\"font-family: 'courier new', courier;\">T{e}<\/span> to construct a value is quite apparent. In contrast to&nbsp;<span style=\"font-family: 'courier new', courier;\">T(e) or (T)e, T{e}<\/span> does not allow narrowing conversion. Narrowing conversion is a conversion including the loss of data accuracy. I assume this is, most of the time, not your intention. Have a look at the example from the guidelines.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">use<\/span>(<span style=\"color: #007788; font-weight: bold;\">char<\/span> ch, <span style=\"color: #007788; font-weight: bold;\">double<\/span> d, <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> p, <span style=\"color: #007788; font-weight: bold;\">long<\/span> <span style=\"color: #007788; font-weight: bold;\">long<\/span> lng){\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>{ch};     <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, but redundant<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>{d};      <span style=\"color: #0099ff; font-style: italic;\">\/\/ error: double-&gt;int narrowing; use a cast if you need to<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x3 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>{p};      <span style=\"color: #0099ff; font-style: italic;\">\/\/ error: pointer to-&gt;int; use a reinterpret_cast if you really need to<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x4 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>{lng};    <span style=\"color: #0099ff; font-style: italic;\">\/\/ error: long long-&gt;int narrowing; use a cast if you need to          (1)<\/span>\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>(ch);     <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, but redundant<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>(d);      <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: double-&gt;int narrowing; use a cast if you need to<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y3 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>(p);      <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: pointer to-&gt;int; use a reinterpret_cast if you really need to  (2)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> y4 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span>(lng);    <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: long-&gt;int narrowing; use a cast if you need to<\/span>\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z1 <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span>)ch;     <span style=\"color: #0099ff; font-style: italic;\">\/\/ OK, but redundant<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z2 <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span>)d;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: double-&gt;int narrowing; use a cast if you need to<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z3 <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span>)p;      <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: pointer to-&gt;int; use a reinterpret_cast if you really need to  (3)<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> z4 <span style=\"color: #555555;\">=<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span>)lng;    <span style=\"color: #0099ff; font-style: italic;\">\/\/ bad: long long-&gt;int narrowing; use a cast if you need to            <\/span>\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;Here is what Gcc provides without any special flags.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5393\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Screenshot_20180223_192512.png\" alt=\"Screenshot 20180223 192512\" width=\"600\" height=\"232\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Screenshot_20180223_192512.png 1136w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Screenshot_20180223_192512-300x116.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Screenshot_20180223_192512-1024x397.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/Screenshot_20180223_192512-768x297.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>If you carefully read the output of the compiler run, you will observe a few interesting facts.<\/p>\n<ul>\n<li>Expression (1) will only give a warning in the first code block; the two previous expressions will produce an error.<\/li>\n<li>Only the expressions (2) and (3) result in an error. The other conversions in&nbsp;the second and third code block will not even give a warning.<\/li>\n<\/ul>\n<p>You must keep a particular rule in mind if you construct a value with <span style=\"font-family: 'courier new', courier;\">T(e1, e2)<\/span>&nbsp;or&nbsp;<span style=\"font-family: 'courier new', courier;\">T{e1, e2}<\/span>. What will happen if you have a class that has two competing constructors? One constructor accepting two ints (<span style=\"font-family: 'courier new', courier;\">MyVector(int, int))<\/span> and the other accepting an <span style=\"font-family: 'courier new', courier;\">std::initializer_list&lt;int&gt; (MyVector(std::initializer_list&lt;int&gt;))? T<\/span>he interesting question is: Does a call <span style=\"font-family: 'courier new', courier;\">MyVector(1, 2)<\/span>&nbsp;or a call <span style=\"font-family: 'courier new', courier;\">MyVector{int, int}<\/span> the constructor for two ints or the one with the <span style=\"font-family: 'courier new', courier;\">std::initalizer_list&lt;int&gt;<\/span>?<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ constructionWithBraces.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyVector<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    MyVector(<span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MyVector(int, int)\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n    MyVector(std<span style=\"color: #555555;\">::<\/span>initializer_list<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MyVector(std::initalizer_list&lt;int&gt;)\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyVector1<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    MyVector1(<span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MyVector1(int, int)\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyVector2<\/span>{\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    MyVector2(<span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">int<\/span>){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"MyVector2(int, int)\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    MyVector(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>);                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    MyVector{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};                       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2) <\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    MyVector1{<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>};                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    MyVector2(<span style=\"color: #ff6600;\">1<\/span>, <span style=\"color: #ff6600;\">2<\/span>);                      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Here is the output of the program. The call (1) calls the constructor with two ints; the call (2) the constructor with the <span style=\"font-family: 'courier new', courier;\">std::initializer_list&lt;int&gt;<\/span>. If you invoke <span style=\"font-family: 'courier new', courier;\">MyVector1{1, 2}<\/span> (3), der constructor <span style=\"font-family: 'courier new', courier;\">MyVector1(1, 2)<\/span> is a kind of fallback.<\/p>\n<p>They will not hold for (4). In this case, the constructor with the <span style=\"font-family: courier new, courier;\">std::initializer_list&lt;int&gt;<\/span> is not the fallback.&nbsp;<\/p>\n<p>&nbsp;&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5394\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError.png\" alt=\"constructionWithBracesError\" width=\"600\" height=\"219\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError.png 1557w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError-300x110.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError-1024x374.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError-768x281.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2018\/02\/constructionWithBracesError-1536x561.png 1536w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>A constructor taking a <span style=\"font-family: 'courier new', courier;\">std::initializer_list<\/span> as an argument is often called a sequence constructor.&nbsp;<\/p>\n<p>Do you know why I called the class in the example <span style=\"font-family: 'courier new', courier;\">MyVector<\/span>? The reason is that the two following expressions behave differently.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> vec(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">1<\/span>);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ ten elements with 1<\/span>\r\nstd<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> vec2{<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">1<\/span>}; <span style=\"color: #0099ff; font-style: italic;\">\/\/ two elements 10 and 1<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The first line creates a vector of 10 elements with the value 1; the second line will create a vector with the values 10 and 1.<\/p>\n<\/p>\n<h3><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-deref\" style=\"color: #268bd2; text-decoration: none;\">ES.65: Don\u2019t dereference an invalid pointer<\/a><\/h3>\n<p>Let me put it this way. If you dereference an invalid pointer, such as a <span style=\"font-family: 'courier new', courier;\">nullptr<\/span>, your program has undefined behavior. This is nasty. The only way to avoid this is to check your pointer before its usage.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">func<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span> p) {\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (p <span style=\"color: #555555;\">==<\/span> nullptr) { <span style=\"color: #0099ff; font-style: italic;\">\/\/ do something special<\/span>\r\n    }\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> x <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">*<\/span>p;\r\n    ...\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>How can you overcome this issue? Don&#8217;t use a naked pointer. Use a smart pointer such as <span style=\"font-family: 'courier new', courier;\">std::unique_ptr<\/span> or <span style=\"font-family: 'courier new', courier;\">std::shared_ptr<\/span> or a reference.&nbsp; I have already written a post on the different kinds of ownership semantics in modern C++. Read the details here: <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-rules-to-resource-management\">C++ Core Guidelines: Rules to Resource Management.<\/a><\/p>\n<p>Let&#8217;s switch gears.&nbsp;<\/p>\n<h2>Rule for statements<\/h2>\n<p>The statement rules are pretty obvious; therefore, I can make it short.<\/p>\n<ul>\n<li>It will help if you prefer a <span style=\"font-family: 'courier new', courier;\">switch s<\/span>tatement to an <span style=\"font-family: courier new, courier;\">if <\/span>statement when there is a choice&nbsp;(<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-switch-if\" style=\"color: #268bd2; text-decoration: none;\">ES.70)&nbsp;<\/a>because a <span style=\"font-family: 'courier new', courier;\">switch <\/span>statement may be more readable and can be better optimized.<\/li>\n<li>The same holds for a range-based for-loop (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-for-range\" style=\"color: #268bd2; text-decoration: none;\">ES.71<\/a>) compared to a for-loop. First, a range-based for loop is easier to read, and second, you can not make an index error or change the index while looping.<\/li>\n<li>When you have an obvious loop variable, you should use a for-loop instead of a <span style=\"font-family: 'courier new', courier;\">while statement<\/span> (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-for-while\" style=\"color: #268bd2; text-decoration: none;\">ES.72<\/a>); if not, you should use a <span style=\"font-family: 'courier new', courier;\">while statement<\/span> (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-while-for\" style=\"color: #268bd2; text-decoration: none;\">ES.73<\/a>).<\/li>\n<\/ul>\n<p>(1) shows an example of when you should prefer a for loop and (2) when you should prefer a <span style=\"font-family: 'courier new', courier;\">while statement<\/span>.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">for<\/span> (gsl<span style=\"color: #555555;\">::<\/span>index i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> vec.size(); i<span style=\"color: #555555;\">++<\/span>) {  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ do work<\/span>\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> events <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;                                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">while<\/span> (wait_for_event()) {   \r\n    <span style=\"color: #555555;\">++<\/span>events;\r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ ...<\/span>\r\n}<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>You should declare a loop variable in a for-loop (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-for-init\" style=\"color: #268bd2; text-decoration: none;\">ES.74). <\/a>This will hold for a for-loop and since C++17 for an <span style=\"font-family: 'courier new', courier;\">if<\/span>&#8211; or <span style=\"font-family: 'courier new', courier;\">switch<\/span>-statement. Read the details here: <a href=\"https:\/\/www.modernescpp.com\/index.php\/cpp17-core\">C++17 &#8211; What&#8217;s new in the core language?<\/a><\/li>\n<li>Avoid <span style=\"font-family: 'courier new', courier;\">do<\/span>-statements (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-do\" style=\"color: #268bd2; text-decoration: none;\">ES.75<\/a>) and <span style=\"font-family: 'courier new', courier;\">goto<\/span>-statements (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-goto\" style=\"color: #268bd2; text-decoration: none;\">ES.76<\/a>), and minimize the use of <span style=\"font-family: 'courier new', courier;\">break<\/span> and <span style=\"font-family: 'courier new', courier;\">continue<\/span> in loops (<a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Res-continue\" style=\"color: #268bd2; text-decoration: none;\">ES.77<\/a>) because they are difficult to read. If something is difficult to read, it&#8217;s also error-prone.<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>There are a few rules for statements left. My <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-to-switch-or-not-to-switch-that-is-the-question\">next post<\/a> will start with them. Afterward, the arithmetic rules become more thrilling.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I continue with the roughly 15 rules for statements, let me finish the two rules for expressions. Both rules help you to protect your program from undefined behavior.&nbsp;<\/p>\n","protected":false},"author":21,"featured_media":5392,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[492],"class_list":["post-5395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-statements"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5395"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5395\/revisions"}],"predecessor-version":[{"id":6838,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5395\/revisions\/6838"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5392"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}