<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>If on Cheat Sheet</title><link>/tags/if/</link><description>Recent content in If on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/if/index.xml" rel="self" type="application/rss+xml"/><item><title>Statements</title><link>/programming/cpp/primer/statements/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/statements/</guid><description>&lt;h1 id="statements"&gt;Statements&lt;a class="anchor" href="#statements"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Key statement syntax and critical caveats based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 5 (Statements).&lt;/p&gt;
&lt;h2 id="1-the-if-statement-and-dangling-else"&gt;1. The &lt;code&gt;if&lt;/code&gt; Statement and Dangling Else&lt;a class="anchor" href="#1-the-if-statement-and-dangling-else"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C++, an &lt;code&gt;else&lt;/code&gt; branch is syntactically paired with the &lt;strong&gt;closest preceding unmatched &lt;code&gt;if&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;if&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;condition1&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;if&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;condition2&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; DoSomething&lt;span style="color:#1f2328"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;else&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Warning: pairs with condition2&amp;#39;s if, not condition1!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; DoAnother&lt;span style="color:#1f2328"&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To eliminate ambiguity and prevent logical bugs, always use explicit curly braces (&lt;code&gt;{}&lt;/code&gt;) to wrap compound statement blocks.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="2-the-switch-statement-and-bypassing-variable-initialization"&gt;2. The &lt;code&gt;switch&lt;/code&gt; Statement and Bypassing Variable Initialization&lt;a class="anchor" href="#2-the-switch-statement-and-bypassing-variable-initialization"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The curly braces &lt;code&gt;{ ... }&lt;/code&gt; wrapping the body of a &lt;code&gt;switch&lt;/code&gt; statement form a single scope. A &lt;code&gt;case&lt;/code&gt; label is merely a jump target and does not establish a separate nested scope.&lt;/p&gt;</description></item></channel></rss>