C++ Statements: Declarations, Expressions, Compound Blocks, and More
What is C++ Statements: Declarations, Expressions, Compound Blocks, and More? Every C++ program is made up of instructions that tell the computer what to do. These instructions are called statements.For example, when you create a variable...
How does C++ categorize statements into declarations, expression statements, and compound blocks with scoped lifecycles?
Explanation & In-Depth Details
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"My age is: "</span> <span class="js-op"><<</span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">My</span> <span class="js-id">age</span> <span class="js-id">is</span><span class="js-op">:</span> <span class="js-num">25</span></code></pre></div><p>In this program:</p><ul><li><p><code>int age = 25;</code> is a declaration statement.</p></li><li><p><code>cout << "My age is: " << age;</code> is an expression statement.</p></li><li><p><code>return 0;</code> is a jump statement.</p></li><li><p>The instructions inside <code>main()</code> form a compound statement.</p></li></ul><p>Each statement performs a specific task and helps the program execute correctly.</p><h2>Main Types of Statements in C++</h2><p>C++ provides several types of statements. Each type has a different purpose in a program.</p><p>The main categories are:</p><ol start="1"><li><p>Declaration statements</p></li><li><p>Expression statements</p></li><li><p>Compound statements (blocks)</p></li><li><p>Selection statements</p></li><li><p>Iteration statements</p></li><li><p>Jump statements</p></li><li><p>Labeled statements</p></li><li><p>Try-blocks</p></li></ol><p>Let's understand each type with simple explanations and practical examples.</p><h2>1. Declaration Statements in C++</h2><p>A declaration statement is used to introduce a variable, function, or other entity into a program.</p><p>It tells the compiler the name and type of an entity so that it can be used in the appropriate scope.</p><p>Variables are commonly declared using data types such as <code>int</code>, <code>float</code>, <code>double</code>, <code>char</code>, and <code>bool</code>.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span> <span class="js-id">variable_name</span><span class="js-op">;</span></code></pre></div><p>You can also declare and initialize a variable in the same statement.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">data_type</span> <span class="js-id">variable_name</span> <span class="js-op">=</span> <span class="js-id">value</span><span class="js-op">;</span></code></pre></div><h3>Example: Variable Declaration</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">float</span> <span class="js-id">marks</span> <span class="js-op">=</span> <span class="js-num">85.5</span><span class="js-op">;</span>
<span class="js-id">char</span> <span class="js-id">grade</span> <span class="js-op">=</span> <span class="js-str">'A'</span><span class="js-op">;</span>
<span class="js-id">double</span> <span class="js-id">salary</span> <span class="js-op">=</span> <span class="js-num">35000.75</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Age: "</span> <span class="js-op"><<</span> <span class="js-id">age</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Marks: "</span> <span class="js-op"><<</span> <span class="js-id">marks</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Grade: "</span> <span class="js-op"><<</span> <span class="js-id">grade</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Salary: "</span> <span class="js-op"><<</span> <span class="js-id">salary</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Age</span><span class="js-op">:</span> <span class="js-num">25</span>
<span class="js-id">Marks</span><span class="js-op">:</span> <span class="js-num">85.5</span>
<span class="js-id">Grade</span><span class="js-op">:</span> <span class="js-id">A</span>
<span class="js-id">Salary</span><span class="js-op">:</span> <span class="js-num">35000.75</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p><code>int age = 25;</code> declares an integer variable.</p></li><li><p><code>float marks = 85.5;</code> declares a floating-point variable.</p></li><li><p><code>char grade = 'A';</code> declares a character variable.</p></li><li><p><code>double salary = 35000.75;</code> declares a double-precision variable.</p></li></ul><p>All four lines are declaration statements.</p><h3>Example: Multiple Variable Declarations</h3><p>You can declare multiple variables of the same type in a single declaration.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">,</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">,</span> <span class="js-id">c</span> <span class="js-op">=</span> <span class="js-num">30</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span> <span class="js-op">+</span> <span class="js-id">c</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">60</span></code></pre></div><h3>Where Are Declaration Statements Used?</h3><p>Declaration statements are used when:</p><ul><li><p>Creating variables.</p></li><li><p>Defining constants.</p></li><li><p>Declaring references.</p></li><li><p>Declaring pointers.</p></li><li><p>Introducing objects of classes.</p></li><li><p>Defining local variables inside functions and blocks.</p></li></ul><h2>2. Expression Statements in C++</h2><p>An expression statement is a statement formed from an expression followed by a semicolon.</p><p>Expressions can perform calculations, assign values, call functions, or modify variables.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">expression</span><span class="js-op">;</span></code></pre></div><h3>Example: Assignment Expression Statement</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Number: "</span> <span class="js-op"><<</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">100</span></code></pre></div><p>Here, <code>number = 100;</code> is an expression statement because it assigns a value to the variable.</p><h3>Example: Arithmetic Expression Statement</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Sum: "</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Sum</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><p>The calculation <code>a + b</code> is an expression used within a declaration statement.</p><h3>Example: Function Call Expression Statement</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-kw">void</span> <span class="js-id">display</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Welcome to C++!"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">display</span><span class="js-op">();</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Welcome</span> <span class="js-id">to</span> <span class="js-id">C</span><span class="js-op">++!</span></code></pre></div><p>The statement <code>display();</code> is an expression statement because it calls a function.</p><h3>Important: Null Statement</h3><p>A semicolon by itself is called a null statement.</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-op">;</span></code></pre></div><p>It performs no operation. It can be useful in situations where the syntax requires a statement but no action is needed.</p><p>However, an accidental extra semicolon after a condition or loop can cause unexpected behaviour.</p><h2>3. Compound Statements in C++ (Blocks)</h2><p>A compound statement is a group of statements enclosed within curly braces <code>{ }</code>.</p><p>It is also called a block statement.</p><p>Compound statements allow you to combine multiple instructions into a single statement.</p><p>They are commonly used with functions, loops, conditional statements, and other control structures.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-op">{</span>
<span class="js-id">statement1</span><span class="js-op">;</span>
<span class="js-id">statement2</span><span class="js-op">;</span>
<span class="js-id">statement3</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>Example: Compound Statement</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">sum</span> <span class="js-op">=</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Sum: "</span> <span class="js-op"><<</span> <span class="js-id">sum</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Sum</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><h3>Explanation</h3><p>In this example:</p><ul><li><p>The opening curly brace <code>{</code> starts a block.</p></li><li><p>The variables <code>a</code> and <code>b</code> are declared inside the block.</p></li><li><p>The calculation is performed inside the same block.</p></li><li><p>The closing curly brace <code>}</code> ends the block.</p></li></ul><p>The variables declared inside the block are local to that block and cannot normally be accessed outside it.</p><h3>Example: Multiple Statements Inside a Block</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">number</span> <span class="js-op">></span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Positive Number"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">number</span> <span class="js-op">=</span> <span class="js-id">number</span> <span class="js-op">+</span> <span class="js-num">5</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Updated Number: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Positive</span> <span class="js-id">Number</span>
<span class="js-id">Updated</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">15</span></code></pre></div><p>The statements inside the <code>if</code> block are executed together when the condition is true.</p><h3>Benefits of Compound Statements</h3><ul><li><p>Group multiple instructions together.</p></li><li><p>Organize program logic.</p></li><li><p>Control variable scope.</p></li><li><p>Make conditional statements and loops easier to write.</p></li><li><p>Improve code readability.</p></li></ul><h2>4. Selection Statements in C++</h2><p>Selection statements allow a program to make decisions based on conditions.</p><p>They determine which block of code should execute depending on whether a condition is true or false.</p><p>C++ provides the following main selection statements:</p><ol start="1"><li><p><code>if</code></p></li><li><p><code>if-else</code></p></li><li><p><code>else-if</code> ladder</p></li><li><p>Nested <code>if</code></p></li><li><p><code>switch</code></p></li></ol><h3>A. if Statement</h3><p>The <code>if</code> statement executes a block of code when a specified condition is true.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">condition</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// Code executes when condition is true</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">>=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"You are eligible to vote."</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">You</span> <span class="js-id">are</span> <span class="js-id">eligible</span> <span class="js-id">to</span> <span class="js-id">vote</span><span class="js-op">.</span></code></pre></div><p>The condition <code>age >= 18</code> is true, so the program executes the code inside the <code>if</code> block.</p><h3>B. if-else Statement</h3><p>The <code>if-else</code> statement provides two possible execution paths.</p><p>If the condition is true, the <code>if</code> block runs. Otherwise, the <code>else</code> block runs.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">condition</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// True condition</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-cm">// False condition</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">7</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">number</span> <span class="js-op">%</span> <span class="js-num">2</span> <span class="js-op">==</span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Even Number"</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Odd Number"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Odd</span> <span class="js-id">Number</span></code></pre></div><h3>C. else-if Ladder</h3><p>An <code>else-if</code> ladder is used when you need to check multiple conditions.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">marks</span> <span class="js-op">=</span> <span class="js-num">85</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">marks</span> <span class="js-op">>=</span> <span class="js-num">90</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Grade A+"</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">marks</span> <span class="js-op">>=</span> <span class="js-num">80</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Grade A"</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">marks</span> <span class="js-op">>=</span> <span class="js-num">70</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Grade B"</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Grade C"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Grade</span> <span class="js-id">A</span></code></pre></div><p>The program checks each condition in order and executes the first matching block.</p><h3>D. Nested if Statement</h3><p>A nested <code>if</code> statement means placing one <code>if</code> statement inside another <code>if</code> statement.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">bool</span> <span class="js-id">hasLicense</span> <span class="js-op">=</span> <span class="js-num">true</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">>=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">hasLicense</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"You can drive."</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"You need a driving license."</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"You are not old enough to drive."</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">You</span> <span class="js-id">can</span> <span class="js-id">drive</span><span class="js-op">.</span></code></pre></div><h3>E. switch Statement</h3><p>The <code>switch</code> statement is used when you want to select one option from several possible values.</p><p>It commonly works with integral types, enumeration types, and certain other types convertible to an integral or enumeration type.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">switch</span> <span class="js-op">(</span><span class="js-id">expression</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">case</span> <span class="js-id">value1</span><span class="js-op">:</span>
<span class="js-cm">// Code</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">case</span> <span class="js-id">value2</span><span class="js-op">:</span>
<span class="js-cm">// Code</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">default</span><span class="js-op">:</span>
<span class="js-cm">// Default code</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">day</span> <span class="js-op">=</span> <span class="js-num">3</span><span class="js-op">;</span>
<span class="js-kw">switch</span> <span class="js-op">(</span><span class="js-id">day</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">case</span> <span class="js-num">1</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Monday"</span><span class="js-op">;</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">case</span> <span class="js-num">2</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Tuesday"</span><span class="js-op">;</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">case</span> <span class="js-num">3</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Wednesday"</span><span class="js-op">;</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">default</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Invalid Day"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Wednesday</span></code></pre></div><p>The <code>break</code> statement prevents execution from continuing into the next case.</p><h3>Where Are Selection Statements Used?</h3><ul><li><p>Checking user eligibility.</p></li><li><p>Calculating grades.</p></li><li><p>Validating user input.</p></li><li><p>Creating menu-driven applications.</p></li><li><p>Handling different program conditions.</p></li><li><p>Selecting operations in calculators.</p></li></ul><h2>5. Iteration Statements in C++ (Loops)</h2><p>Iteration statements are used to repeat a block of code multiple times.</p><p>They are also called looping statements.</p><p>C++ provides three main types of loops:</p><ol start="1"><li><p><code>for</code> loop</p></li><li><p><code>while</code> loop</p></li><li><p><code>do-while</code> loop</p></li></ol><p>These loops help reduce repeated code and make programs more efficient.</p><h3>A. for Loop</h3><p>A <code>for</code> loop is commonly used when you know how many times you want to repeat an operation.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">initialization</span><span class="js-op">;</span> <span class="js-id">condition</span><span class="js-op">;</span> <span class="js-id">update</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// Code to repeat</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">3</span>
<span class="js-num">4</span>
<span class="js-num">5</span></code></pre></div><h3>Explanation</h3><ul><li><p>Initialization sets the starting value.</p></li><li><p>Condition determines whether the loop should continue.</p></li><li><p>Update changes the variable after each iteration.</p></li></ul><h3>B. while Loop</h3><p>A <code>while</code> loop executes a block of code as long as its condition remains true.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">condition</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// Code to repeat</span>
<span class="js-op">}</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">i</span><span class="js-op">++;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">3</span>
<span class="js-num">4</span>
<span class="js-num">5</span></code></pre></div><p>The condition is checked before each iteration. If it is false initially, the loop body will not execute.</p><h3>C. do-while Loop</h3><p>A <code>do-while</code> loop executes the code block first and checks the condition afterward.</p><p>This means the loop body executes at least once.</p><p><strong>Syntax:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-cm">// Code to repeat</span>
<span class="js-op">}</span> <span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">condition</span><span class="js-op">);</span></code></pre></div><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">i</span><span class="js-op">++;</span>
<span class="js-op">}</span> <span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">);</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">3</span>
<span class="js-num">4</span>
<span class="js-num">5</span></code></pre></div><h3>Difference Between for, while, and do-while Loops</h3><table><tbody><tr><th>Feature</th><th>for</th><th>while</th><th>do-while</th></tr><tr><td>Condition checking</td><td>Before execution</td><td>Before execution</td><td>After execution</td></tr><tr><td>Minimum executions</td><td>Zero</td><td>Zero</td><td>One</td></tr><tr><td>Best use</td><td>Known iteration count</td><td>Condition-based repetition</td><td>At least one execution required</td></tr><tr><td>Syntax</td><td>Initialization, condition, update</td><td>Condition</td><td>Code followed by condition</td></tr></tbody></table><h2>6. Jump Statements in C++</h2><p>Jump statements change the normal flow of program execution.</p><p>They are used to exit loops, skip iterations, return from functions, or transfer control to another location.</p><p>C++ provides several jump statements:</p><ol start="1"><li><p><code>break</code></p></li><li><p><code>continue</code></p></li><li><p><code>return</code></p></li><li><p><code>goto</code></p></li></ol><h3>A. break Statement</h3><p>The <code>break</code> statement immediately terminates the nearest enclosing loop or <code>switch</code> statement.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">10</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op">==</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">3</span>
<span class="js-num">4</span></code></pre></div><p>When <code>i</code> becomes 5, the <code>break</code> statement terminates the loop.</p><h3>B. continue Statement</h3><p>The <code>continue</code> statement skips the remaining statements in the current loop iteration and moves to the next iteration.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op">==</span> <span class="js-num">3</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">continue</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-num">1</span>
<span class="js-num">2</span>
<span class="js-num">4</span>
<span class="js-num">5</span></code></pre></div><p>The number 3 is skipped because the <code>continue</code> statement moves execution to the next iteration.</p><h3>C. return Statement</h3><p>The <code>return</code> statement ends the execution of a function and can send a value back to the calling function.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">add</span><span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">a</span><span class="js-op">,</span> <span class="js-id">int</span> <span class="js-id">b</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">return</span> <span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">result</span> <span class="js-op">=</span> <span class="js-id">add</span><span class="js-op">(</span><span class="js-num">10</span><span class="js-op">,</span> <span class="js-num">20</span><span class="js-op">);</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Sum: "</span> <span class="js-op"><<</span> <span class="js-id">result</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Sum</span><span class="js-op">:</span> <span class="js-num">30</span></code></pre></div><p>The <code>return a + b;</code> statement sends the calculated result back to <code>main()</code>.</p><h3>D. goto Statement</h3><p>The <code>goto</code> statement transfers program execution to a labeled statement within the same function.</p><p><strong>Example:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">number</span> <span class="js-op">==</span> <span class="js-num">1</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">goto</span> <span class="js-id">message</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"This line is skipped."</span><span class="js-op">;</span>
<span class="js-id">message</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Welcome to C++!"</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Welcome</span> <span class="js-id">to</span> <span class="js-id">C</span><span class="js-op">++!</span></code></pre></div><p>The <code>goto</code> statement transfers control directly to the <code>message</code> label.</p><p><strong>Important:</strong> Although <code>goto</code> is supported in C++, it should generally be avoided in ordinary programming because it can make code difficult to understand. Loops, functions, and conditional statements are usually better choices.</p><h2>7. Labeled Statements in C++</h2><p>A labeled statement is a statement that has a label attached to it.</p><p>Labels are mainly used with <code>goto</code> and <code>case</code> or <code>default</code> labels in a <code>switch</code> statement.</p><h3>Types of Labels</h3><ul><li><p>Identifier labels</p></li><li><p><code>case</code> labels</p></li><li><p><code>default</code> labels</p></li></ul><h3>Example: Identifier Label</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">start</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Number: "</span> <span class="js-op"><<</span> <span class="js-id">number</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">10</span></code></pre></div><p>Here, <code>start:</code> is an identifier label.</p><p>It marks a location in the function that can be targeted by a <code>goto</code> statement.</p><h3>Example: Case and Default Labels</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">choice</span> <span class="js-op">=</span> <span class="js-num">2</span><span class="js-op">;</span>
<span class="js-kw">switch</span> <span class="js-op">(</span><span class="js-id">choice</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">case</span> <span class="js-num">1</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Option One"</span><span class="js-op">;</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">case</span> <span class="js-num">2</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Option Two"</span><span class="js-op">;</span>
<span class="js-kw">break</span><span class="js-op">;</span>
<span class="js-kw">default</span><span class="js-op">:</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Invalid Option"</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Option</span> <span class="js-id">Two</span></code></pre></div><p>The <code>case</code> and <code>default</code> labels identify the different execution paths inside the <code>switch</code> statement.</p><h2>8. Try-Blocks in C++</h2><p>A try-block is used for exception handling in C++.</p><p>It allows you to execute code that might generate an exception and handle the problem using a <code>catch</code> block.</p><p>Try-blocks help prevent certain errors from unexpectedly terminating program execution.</p><h3>Syntax</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">try</span> <span class="js-op">{</span>
<span class="js-cm">// Code that may throw an exception</span>
<span class="js-op">}</span> <span class="js-kw">catch</span> <span class="js-op">(</span><span class="js-id">exception_type</span> <span class="js-id">e</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-cm">// Handle the exception</span>
<span class="js-op">}</span></code></pre></div><h3>Example: Using try and catch</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">stdexcept</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-op">-</span><span class="js-num">5</span><span class="js-op">;</span>
<span class="js-kw">try</span> <span class="js-op">{</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op"><</span> <span class="js-num">0</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">throw</span> <span class="js-id">invalid_argument</span><span class="js-op">(</span><span class="js-str">"Age cannot be negative."</span><span class="js-op">);</span>
<span class="js-op">}</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Age: "</span> <span class="js-op"><<</span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-op">}</span> <span class="js-kw">catch</span> <span class="js-op">(</span><span class="js-kw">const</span> <span class="js-id">invalid_argument</span><span class="js-op">&</span> <span class="js-id">e</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cerr</span> <span class="js-op"><<</span> <span class="js-str">"Error: "</span> <span class="js-op"><<</span> <span class="js-id">e</span><span class="js-op">.</span><span class="js-id">what</span><span class="js-op">();</span>
<span class="js-op">}</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p><strong>Output:</strong></p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Error</span><span class="js-op">:</span> <span class="js-id">Age</span> <span class="js-id">cannot</span> <span class="js-id">be</span> <span class="js-id">negative</span><span class="js-op">.</span></code></pre></div><h3>Explanation</h3><ul><li><p>The <code>try</code> block contains code that may throw an exception.</p></li><li><p>The <code>throw</code> statement raises an exception when the condition is invalid.</p></li><li><p>The <code>catch</code> block handles the exception.</p></li><li><p><code>e.what()</code> provides the error message.</p></li></ul><p>Try-blocks are especially useful when working with file operations, input validation, and applications that need structured error handling.</p><h2>Difference Between Statements and Expressions in C++</h2><p>Statements and expressions are related, but they are not the same.</p><p>An expression produces a value or performs an operation. A statement is a complete instruction that performs an action.</p><table><tbody><tr><th>Feature</th><th>Expression</th><th>Statement</th></tr><tr><td>Meaning</td><td>Represents a calculation or operation</td><td>Complete instruction</td></tr><tr><td>Main purpose</td><td>Produces a value or effect</td><td>Performs a program action</td></tr><tr><td>Example</td><td><code>a + b</code></td><td><code>a = b;</code></td></tr><tr><td>Semicolon</td><td>Depends on where it is used</td><td>Many simple statements end with one</td></tr><tr><td>Usage</td><td>Used inside statements and other expressions</td><td>Forms the structure of a program</td></tr></tbody></table><h3>Example</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">b</span> <span class="js-op">=</span> <span class="js-num">20</span><span class="js-op">;</span>
<span class="js-id">a</span> <span class="js-op">+</span> <span class="js-id">b</span><span class="js-op">;</span></code></pre></div><p>Here:</p><ul><li><p><code>a + b</code> is an expression.</p></li><li><p><code>a + b;</code> is an expression statement.</p></li><li><p><code>int a = 10;</code> is a declaration statement.</p></li></ul><p>Understanding this difference helps you read and write C++ code more effectively.</p><h2>Semicolons and Curly Braces in C++</h2><p>Two important symbols used when writing C++ statements are semicolons and curly braces.</p><h3>1. Semicolon (;)</h3><p>A semicolon marks the end of many simple statements.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">age</span><span class="js-op">;</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span></code></pre></div><h3>2. Curly Braces { }</h3><p>Curly braces define a compound statement or block.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">>=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Adult"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The statements inside the braces belong to the <code>if</code> block.</p><p><strong>Important:</strong> Not every C++ statement ends with a semicolon directly. For example, an <code>if</code>, <code>for</code>, or <code>while</code> statement followed by a compound block does not require a semicolon after the closing brace.</p><h2>Complete C++ Program Using Different Statements</h2><p>Let's combine declaration, expression, selection, iteration, compound, and jump statements in one program.</p><h3>C++ Code</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code>#<span class="js-id">include</span> <span class="js-op"><</span><span class="js-id">iostream</span><span class="js-op">></span>
<span class="js-id">using</span> <span class="js-id">namespace</span> <span class="js-id">std</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">main</span><span class="js-op">()</span> <span class="js-op">{</span>
<span class="js-cm">// Declaration statement</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">5</span><span class="js-op">;</span>
<span class="js-id">int</span> <span class="js-id">sum</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-cm">// Compound statement</span>
<span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Starting Number: "</span> <span class="js-op"><<</span> <span class="js-id">number</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-cm">// Iteration statement</span>
<span class="js-kw">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-id">number</span><span class="js-op">;</span> <span class="js-id">i</span><span class="js-op">++)</span> <span class="js-op">{</span>
<span class="js-cm">// Selection statement</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op">==</span> <span class="js-num">3</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-kw">continue</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-cm">// Expression statement</span>
<span class="js-id">sum</span> <span class="js-op">+=</span> <span class="js-id">i</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-cm">// Output statement</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Total Sum: "</span> <span class="js-op"><<</span> <span class="js-id">sum</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-cm">// Jump statement</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><h3>Output</h3><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">Starting</span> <span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">5</span>
<span class="js-id">Total</span> <span class="js-id">Sum</span><span class="js-op">:</span> <span class="js-num">12</span></code></pre></div><h3>Explanation</h3><p>This program demonstrates several types of C++ statements:</p><ol start="1"><li><p>Declaration statements create the variables <code>number</code> and <code>sum</code>.</p></li><li><p>A compound statement groups instructions inside curly braces.</p></li><li><p>A <code>for</code> statement repeats the calculation.</p></li><li><p>An <code>if</code> statement checks whether the current number is 3.</p></li><li><p>A <code>continue</code> statement skips that iteration.</p></li><li><p>An expression statement updates the sum.</p></li><li><p>The <code>return</code> statement ends the <code>main()</code> function.</p></li></ol><p>This example shows how different statements work together in a single C++ program.</p><h2>Common Mistakes When Using Statements in C++</h2><p>Beginners sometimes make mistakes while writing C++ statements. Understanding these mistakes will help you write cleaner programs.</p><h3>1. Forgetting the Semicolon</h3><p>Incorrect:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span></code></pre></div><p>Correct:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">age</span> <span class="js-op">=</span> <span class="js-num">25</span><span class="js-op">;</span></code></pre></div><p>Most simple statements require a semicolon at the end.</p><h3>2. Missing Curly Braces</h3><p>Incorrect:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">>=</span> <span class="js-num">18</span><span class="js-op">)</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Adult"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Eligible"</span><span class="js-op">;</span></code></pre></div><p>Only the first output statement belongs to the <code>if</code> condition.</p><p>Correct:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">>=</span> <span class="js-num">18</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Adult"</span><span class="js-op">;</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Eligible"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Use braces to group multiple instructions when they should execute together.</p><h3>3. Confusing Assignment and Comparison</h3><p>Incorrect:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Ten"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Correct:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">number</span> <span class="js-op">==</span> <span class="js-num">10</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Ten"</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>Use <code>==</code> when comparing values and <code>=</code> when assigning values.</p><h3>4. Creating an Infinite Loop</h3><p>Incorrect:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span><span class="js-op">;</span>
<span class="js-op">}</span></code></pre></div><p>The variable <code>i</code> is never updated, so the condition remains true.</p><p>Correct:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">i</span> <span class="js-op"><=</span> <span class="js-num">5</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-id">i</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-id">i</span><span class="js-op">++;</span>
<span class="js-op">}</span></code></pre></div><h3>5. Using break Outside a Loop or switch</h3><p>The <code>break</code> statement is used to exit the nearest enclosing loop or <code>switch</code> statement. It cannot be used freely inside an ordinary function or block.</p><h3>6. Declaring Variables in the Wrong Scope</h3><p>A variable declared inside a compound block is generally available only within that block and its nested scopes.</p><p>Example:</p><div class="code-card position-relative my-2">
<div class="code-card-actions">
<button type="button" class="btn-code-action copy-code-btn" onclick="copyCode(this)" title="Copy Code">
<i class="far fa-copy"></i>
</button>
<a href="/tools/cpp-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="code-box"><code><span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">number</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-cm">// number cannot be accessed here</span></code></pre></div><p>Always declare variables in the scope where they are needed.</p><h2>Best Practices for Writing C++ Statements</h2><p>Follow these simple practices when writing C++ programs:</p><ul><li><p>Use meaningful variable names.</p></li><li><p>End simple statements with semicolons.</p></li><li><p>Use curly braces to organize compound blocks.</p></li><li><p>Keep indentation consistent.</p></li><li><p>Use selection statements for decision-making.</p></li><li><p>Choose the correct loop for repeated operations.</p></li><li><p>Avoid unnecessary <code>goto</code> statements.</p></li><li><p>Check conditions carefully before using loops.</p></li><li><p>Keep statements short and readable.</p></li><li><p>Use exception handling when appropriate.</p></li><li><p>Declare variables close to where they are needed.</p></li></ul><h2>Frequently Asked Questions About C++ Statements</h2><h3>1. What is a statement in C++?</h3><p>A statement is a complete instruction that tells a C++ program to perform a specific action, such as declaring a variable, calculating a value, or controlling program execution.</p><h3>2. What are the main types of statements in C++?</h3><p>The main statement categories include declaration, expression, compound, selection, iteration, jump, labeled, and try-block statements.</p><h3>3. What is a declaration statement in C++?</h3><p>A declaration statement introduces a variable, object, or other entity into a program. For example, <code>int age = 25;</code> declares and initializes an integer variable.</p><h3>4. What is an expression statement in C++?</h3><p>An expression statement is an expression followed by a semicolon. Examples include <code>a = 10;</code>, <code>a++;</code>, and <code>display();</code>.</p><h3>5. What is a compound statement in C++?</h3><p>A compound statement is a group of statements enclosed within curly braces <code>{ }</code>. It is also called a block.</p><h3>6. What are selection statements in C++?</h3><p>Selection statements allow a program to make decisions. The main examples are <code>if</code>, <code>if-else</code>, and <code>switch</code>.</p><h3>7. What are iteration statements in C++?</h3><p>Iteration statements are used to repeat code. C++ provides <code>for</code>, <code>while</code>, and <code>do-while</code> loops.</p><h3>8. What are jump statements in C++?</h3><p>Jump statements change the normal flow of execution. Common examples are <code>break</code>, <code>continue</code>, <code>return</code>, and <code>goto</code>.</p><h3>9. What is the difference between a statement and an expression?</h3><p>An expression represents a calculation or operation that produces a value or effect. A statement is a complete instruction that performs an action.</p><h3>10. Why are semicolons used in C++?</h3><p>Semicolons mark the end of many simple C++ statements, including declarations, expression statements, and return statements.</p><h3>11. What are curly braces used for in C++?</h3><p>Curly braces <code>{ }</code> define compound statements or blocks. They group multiple statements and establish a scope for local variables.</p><h3>12. What is a null statement in C++?</h3><p>A null statement is a single semicolon (<code>;</code>). It represents an empty statement that performs no operation.</p><h3>13. What is the purpose of the return statement?</h3><p>The <code>return</code> statement ends the execution of a function. It can also return a value to the calling function.</p><h3>14. What is the purpose of a try-block in C++?</h3><p>A try-block contains code that may throw an exception. A corresponding catch-block handles the exception when one is thrown.</p>
Code Example
#include <iostream>
#include <vector>
int computeScore() { return 85; }
int main() {
// C++17 if statement with initializer
if (int score = computeScore(); score >= 80) {
std::cout << "High performance score achieved: " << score << "
";
} else {
std::cout << "Score below threshold: " << score << "
";
}
// 'score' is automatically out of scope here
// Compound Block Scope
{
std::vector<int> tempBuffer = {1, 2, 3};
std::cout << "Buffer active with " << tempBuffer.size() << " elements
";
} // tempBuffer memory automatically released here
return 0;
}Interview Question & Tips
A: It restricts the scope of helper variables strictly to the `if` and `else` branches, preventing variable leakage into the enclosing function and eliminating name collisions.
Q: How does a compound block {} affect object lifetimes?
A: Any automatic (stack) variables declared inside `{}` are constructed when declared and guaranteed to have their destructors executed when the closing brace `}` is reached.
Best Practices
2. Enclose temporary work in compound `{}` blocks to trigger early RAII resource deallocation.
3. Avoid empty statement semicolons (`;`) that can cause unintended logic bugs.
This study reference for C++ Statements: Declarations, Expressions, Compound Blocks, and More is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: