Jump Statements in C++: break, continue, goto, and return
What is Jump Statements in C++: break, continue, goto, and return? When writing a C++ program, instructions normally execute one after another, from top to bottom. However, sometimes you need to change this normal execution flo...
How do jump statements transfer control and manage RAII stack unwinding in C++?
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-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 the value of <code>i</code> becomes 5, the <code>break</code> statement immediately terminates the loop.</p><p>This is a simple example of how jump statements change program execution.</p><h2>Types of Jump Statements in C++</h2><p>C++ provides four main jump statements.</p><table><tbody><tr><th>Jump statement</th><th>Purpose</th><th>Common use</th></tr><tr><td><code>break</code></td><td>Terminates the nearest loop or switch</td><td>Stop a loop</td></tr><tr><td><code>continue</code></td><td>Skips the current loop iteration</td><td>Skip specific values</td></tr><tr><td><code>goto</code></td><td>Transfers control to a labeled statement</td><td>Special control-flow situations</td></tr><tr><td><code>return</code></td><td>Ends a function</td><td>Return a result or exit a function</td></tr></tbody></table><p>Let's understand each statement with its syntax and practical examples.</p><h2>1. break Statement in C++</h2><p>The <code>break</code> statement is used to immediately terminate the nearest enclosing loop or <code>switch</code> statement.</p><p>Once the <code>break</code> statement executes, the program continues from the first statement after the terminated loop or switch.</p><p>It is commonly used when you want to stop a loop after finding a particular value or when a condition is satisfied.</p><h3>Syntax of break 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-kw">break</span><span class="js-op">;</span></code></pre></div><p>The <code>break</code> statement does not require any condition. It is usually placed inside an <code>if</code> statement when you want to terminate execution based on a specific condition.</p><h3>Example 1: Using break in a for Loop</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-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><h3>Explanation</h3><ul><li><p>The loop starts with <code>i = 1</code>.</p></li><li><p>The condition checks whether <code>i</code> is less than or equal to 10.</p></li><li><p>Numbers are printed one by one.</p></li><li><p>When <code>i</code> becomes 5, the <code>if</code> condition becomes true.</p></li><li><p>The <code>break</code> statement terminates the loop immediately.</p></li></ul><p>The numbers 5 to 10 are not printed.</p><h3>Example 2: Using break in a while Loop</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">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">10</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">6</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-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 loop stops when the value of <code>i</code> reaches 6.</p><h3>Example 3: Using break in a switch Statement</h3><p>The <code>break</code> statement is also commonly used inside a <code>switch</code> 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">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">"Home"</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">"About Us"</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">"Contact"</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 Choice"</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">About</span> <span class="js-id">Us</span></code></pre></div><p>Here, <code>break</code> prevents execution from continuing into the next case.</p><h3>Practical Uses of break</h3><p>The <code>break</code> statement is useful when:</p><ul><li><p>You find the required item in a search operation.</p></li><li><p>A user selects an exit option from a menu.</p></li><li><p>A specific condition requires a loop to stop.</p></li><li><p>You want to exit a <code>switch</code> case.</p></li><li><p>An operation has reached its required result.</p></li></ul><h2>2. continue Statement in C++</h2><p>The <code>continue</code> statement is used to skip the remaining statements in the current loop iteration.</p><p>Unlike <code>break</code>, it does not terminate the entire loop.</p><p>After executing <code>continue</code>, the loop proceeds to its next iteration according to the rules of that loop.</p><p>It is useful when you want to ignore certain values but continue processing the remaining values.</p><h3>Syntax of continue 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-kw">continue</span><span class="js-op">;</span></code></pre></div><p>The <code>continue</code> statement is used inside looping statements such as <code>for</code>, <code>while</code>, and <code>do-while</code>.</p><h3>Example 1: Using continue in a for Loop</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-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><h3>Explanation</h3><ul><li><p>The loop starts with <code>i = 1</code>.</p></li><li><p>The program checks whether <code>i</code> is equal to 3.</p></li><li><p>When the condition is true, <code>continue</code> skips the remaining statements in that iteration.</p></li><li><p>The loop proceeds with the next value.</p></li></ul><p>Therefore, the number 3 is not printed.</p><h3>Example 2: Using continue to Print Even Numbers</h3><p>You can also use <code>continue</code> to skip odd numbers and print only even numbers.</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">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-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">2</span>
<span class="js-num">4</span>
<span class="js-num">6</span>
<span class="js-num">8</span>
<span class="js-num">10</span></code></pre></div><h3>Explanation</h3><p>The modulus operator <code>%</code> checks the remainder after division.</p><ul><li><p>Even numbers produce a remainder of 0 when divided by 2.</p></li><li><p>Odd numbers produce a remainder of 1.</p></li><li><p>The <code>continue</code> statement skips the odd numbers.</p></li></ul><h3>Example 3: Using continue in a while Loop</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">i</span> <span class="js-op">=</span> <span class="js-num">0</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">i</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>Notice that the increment happens before <code>continue</code>. This is important in a <code>while</code> loop because the update must still happen to avoid an infinite loop.</p><h3>Practical Uses of continue</h3><p>The <code>continue</code> statement is useful when:</p><ul><li><p>Skipping invalid records.</p></li><li><p>Ignoring unwanted values.</p></li><li><p>Printing only even or odd numbers.</p></li><li><p>Filtering data inside loops.</p></li><li><p>Processing only records that meet specific conditions.</p></li></ul><h2>3. goto Statement in C++</h2><p>The <code>goto</code> statement is used to transfer program execution directly to a labeled statement within the same function.</p><p>Unlike <code>break</code> and <code>continue</code>, it does not need to be used only with loops or switch statements.</p><p>You can use <code>goto</code> to jump forward or backward to a label in the same function, subject to C++ scope and initialization rules.</p><h3>Syntax of goto 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">goto</span> <span class="js-id">label_name</span><span class="js-op">;</span>
<span class="js-cm">// Other statements</span>
<span class="js-id">label_name</span><span class="js-op">:</span>
<span class="js-cm">// Statement to execute</span></code></pre></div><p>Here:</p><ul><li><p><code>goto</code> is the jump keyword.</p></li><li><p><code>label_name</code> is the name of the destination label.</p></li><li><p>A colon (<code>:</code>) is placed after the label.</p></li></ul><h3>Example 1: Using goto 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-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">10</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 message will be skipped."</span> <span class="js-op"><<</span> <span class="js-id">endl</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++ Programming!"</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> <span class="js-id">Programming</span><span class="js-op">!</span></code></pre></div><h3>Explanation</h3><ul><li><p>The program declares a variable named <code>number</code>.</p></li><li><p>The <code>if</code> condition checks whether the value is 10.</p></li><li><p>Since the condition is true, <code>goto message</code> transfers control to the <code>message</code> label.</p></li><li><p>The statement between the <code>goto</code> and the label is skipped.</p></li><li><p>The program executes the statement after the label.</p></li></ul><h3>Example 2: Using goto to Repeat an Operation</h3><p>The <code>goto</code> statement can also transfer control backward to repeat a section of code.</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-id">start</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-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-id">goto</span> <span class="js-id">start</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>The variable <code>i</code> starts at 1.</p></li><li><p>The program checks whether <code>i</code> is less than or equal to 5.</p></li><li><p>The current value is displayed.</p></li><li><p>The value of <code>i</code> increases by one.</p></li><li><p><code>goto start</code> transfers execution back to the <code>start</code> label.</p></li><li><p>When <code>i</code> becomes 6, the condition becomes false and the program exits the block.</p></li></ul><p>This example demonstrates repetition using <code>goto</code>, although a <code>for</code> or <code>while</code> loop is a better choice for this task.</p><h3>Advantages and Disadvantages of goto</h3><table><tbody><tr><th>Advantages</th><th>Disadvantages</th></tr><tr><td>Can transfer control to a specific label</td><td>Can make code difficult to understand</td></tr><tr><td>Useful in certain low-level control-flow situations</td><td>Can create complicated execution paths</td></tr><tr><td>Can exit deeply nested structures in special cases</td><td>Makes debugging and maintenance harder</td></tr><tr><td>Supported by the C++ language</td><td>Can make program logic less organized</td></tr></tbody></table><p><strong>Important:</strong> Avoid using <code>goto</code> in ordinary programming whenever possible. Prefer loops, functions, and structured control statements because they make code easier to read and maintain.</p><h2>4. return Statement in C++</h2><p>The <code>return</code> statement is used to terminate the execution of a function.</p><p>It can also send a value back to the function that called it.</p><p>The <code>return</code> statement is commonly used in functions that calculate results, perform operations, or provide information to another part of a program.</p><h3>Syntax of return Statement</h3><p>For a function that returns a value:</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">return</span> <span class="js-id">value</span><span class="js-op">;</span></code></pre></div><p>For a function that does not return a value:</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">return</span><span class="js-op">;</span></code></pre></div><p>The value returned must be compatible with the function's declared return type.</p><h3>Example 1: Using return in a Function</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">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><h3>Explanation</h3><ul><li><p>The <code>add()</code> function receives two integer arguments.</p></li><li><p>It calculates their sum.</p></li><li><p><code>return a + b;</code> sends the result back to <code>main()</code>.</p></li><li><p>The returned value is stored in the <code>result</code> variable.</p></li><li><p><code>cout</code> displays the final result.</p></li></ul><h3>Example 2: Using return to Exit a Function Early</h3><p>The <code>return</code> statement can terminate a function before reaching its final 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">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">checkNumber</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-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">"Negative Number"</span><span class="js-op">;</span>
<span class="js-kw">return</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 or Zero"</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">checkNumber</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-id">Negative</span> <span class="js-id">Number</span></code></pre></div><h3>Explanation</h3><p>When the number is negative, the function displays a message and executes <code>return</code>.</p><p>The remaining statements in the function are not executed.</p><h3>Example 3: Using return 0 in main()</h3><p>The <code>main()</code> function is the starting point of a C++ program.</p><p>When <code>main()</code> returns 0, it normally indicates successful program completion to the operating system.</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">cout</span> <span class="js-op"><<</span> <span class="js-str">"Program Started"</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">"Program Completed"</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">Program</span> <span class="js-id">Started</span>
<span class="js-id">Program</span> <span class="js-id">Completed</span></code></pre></div><p><strong>Important:</strong> In C++, reaching the closing brace of <code>main()</code> is equivalent to returning 0. However, writing <code>return 0;</code> explicitly is common in beginner tutorials.</p><h3>Practical Uses of return</h3><p>The <code>return</code> statement is useful when:</p><ul><li><p>Returning calculated values from functions.</p></li><li><p>Ending a function after completing a task.</p></li><li><p>Exiting a function when an invalid condition is detected.</p></li><li><p>Returning status information.</p></li><li><p>Ending the <code>main()</code> function.</p></li></ul><h2>Difference Between break, continue, goto, and return in C++</h2><p>All four jump statements change program execution, but they work differently.</p><h3>Comparison Table</h3><table><tbody><tr><th>Feature</th><th>break</th><th>continue</th><th>goto</th><th>return</th></tr><tr><td>Main purpose</td><td>Stop a loop or switch</td><td>Skip current loop iteration</td><td>Jump to a label</td><td>Exit a function</td></tr><tr><td>Used in loops</td><td>Yes</td><td>Yes</td><td>Can be used</td><td>Can exit a function containing a loop</td></tr><tr><td>Used in switch</td><td>Yes</td><td>No</td><td>Can jump to a label</td><td>Can exit the function</td></tr><tr><td>Requires a label</td><td>No</td><td>No</td><td>Yes</td><td>No</td></tr><tr><td>Returns a value</td><td>No</td><td>No</td><td>No</td><td>Sometimes</td></tr><tr><td>Common use</td><td>Stop execution</td><td>Skip values</td><td>Special control transfer</td><td>Finish a function</td></tr></tbody></table><h3>Simple Example of All Four Jump Statements</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">calculate</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-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-kw">return</span> <span class="js-num">0</span><span class="js-op">;</span>
<span class="js-op">}</span>
<span class="js-kw">return</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-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">// break example</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">4</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-str">" "</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">endl</span><span class="js-op">;</span>
<span class="js-cm">// continue example</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-str">" "</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">endl</span><span class="js-op">;</span>
<span class="js-cm">// goto example</span>
<span class="js-id">goto</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">"Skipped"</span> <span class="js-op"><<</span> <span class="js-id">endl</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">"Goto executed"</span> <span class="js-op"><<</span> <span class="js-id">endl</span><span class="js-op">;</span>
<span class="js-cm">// return example</span>
<span class="js-id">cout</span> <span class="js-op"><<</span> <span class="js-str">"Result: "</span> <span class="js-op"><<</span> <span class="js-id">calculate</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">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-num">1</span> <span class="js-num">2</span> <span class="js-num">3</span>
<span class="js-num">1</span> <span class="js-num">2</span> <span class="js-num">4</span> <span class="js-num">5</span>
<span class="js-id">Goto</span> <span class="js-id">executed</span>
<span class="js-id">Result</span><span class="js-op">:</span> <span class="js-num">20</span></code></pre></div><h3>Understanding the Output</h3><ul><li><p>The first loop stops when <code>i</code> reaches 4 because of <code>break</code>.</p></li><li><p>The second loop skips 3 because of <code>continue</code>.</p></li><li><p>The <code>goto</code> statement transfers control to the <code>message</code> label.</p></li><li><p>The <code>calculate()</code> function returns 20.</p></li><li><p>Finally, <code>return 0</code> ends the <code>main()</code> function successfully.</p></li></ul><h2>Difference Between break and continue in C++</h2><p>Beginners often confuse <code>break</code> and <code>continue</code> because both are used inside loops.</p><p>However, they perform different operations.</p><table><tbody><tr><th>Feature</th><th>break</th><th>continue</th></tr><tr><td>Purpose</td><td>Terminates the nearest loop</td><td>Skips the current iteration</td></tr><tr><td>Loop execution</td><td>Stops completely</td><td>Continues with the next iteration</td></tr><tr><td>Remaining iterations</td><td>Not executed</td><td>Executed normally</td></tr><tr><td>Common use</td><td>Stop when a result is found</td><td>Skip unwanted values</td></tr></tbody></table><h3>Example Using break</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">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">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-str">" "</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></code></pre></div><h3>Example Using continue</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">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-str">" "</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>Remember: <code>break</code> stops the loop, while <code>continue</code> skips only the current iteration.</p><h2>How Jump Statements Work in Nested Loops</h2><p>C++ also allows jump statements to be used inside nested loops.</p><p>A nested loop is a loop placed inside another loop.</p><h3>Example: break in Nested Loops</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-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">3</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">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">j</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">j</span> <span class="js-op"><=</span> <span class="js-num">3</span><span class="js-op">;</span> <span class="js-id">j</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">j</span> <span class="js-op">==</span> <span class="js-num">2</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-str">" "</span> <span class="js-op"><<</span> <span class="js-id">j</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-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">1</span>
<span class="js-num">2</span> <span class="js-num">1</span>
<span class="js-num">3</span> <span class="js-num">1</span></code></pre></div><p>The <code>break</code> statement terminates only the nearest enclosing loop, which is the inner loop. The outer loop continues normally.</p><h3>Example: continue in Nested Loops</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-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">2</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">for</span> <span class="js-op">(</span><span class="js-id">int</span> <span class="js-id">j</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span> <span class="js-id">j</span> <span class="js-op"><=</span> <span class="js-num">3</span><span class="js-op">;</span> <span class="js-id">j</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">j</span> <span class="js-op">==</span> <span class="js-num">2</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-str">" "</span> <span class="js-op"><<</span> <span class="js-id">j</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-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">1</span>
<span class="js-num">1</span> <span class="js-num">3</span>
<span class="js-num">2</span> <span class="js-num">1</span>
<span class="js-num">2</span> <span class="js-num">3</span></code></pre></div><p>Here, the inner loop skips the value 2 during each iteration of the outer loop.</p><h2>Common Mistakes When Using Jump Statements in C++</h2><p>Beginners often make mistakes when working with jump statements. Understanding these problems can help you write better C++ programs.</p><h3>1. Confusing break and continue</h3><p>Incorrect understanding:</p><ul><li><p><code>break</code> skips only one iteration.</p></li><li><p><code>continue</code> stops the entire loop.</p></li></ul><p>This is incorrect.</p><p>Correct understanding:</p><ul><li><p><code>break</code> terminates the nearest enclosing loop or switch.</p></li><li><p><code>continue</code> skips the current loop iteration.</p></li></ul><h3>2. Using continue Outside a Loop</h3><p>The <code>continue</code> statement can only be used within an iteration statement.</p><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">a</span> <span class="js-op">=</span> <span class="js-num">10</span><span class="js-op">;</span>
<span class="js-kw">continue</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">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></code></pre></div><h3>3. Forgetting to Update a Variable After continue in a while 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-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">i</span><span class="js-op">++;</span>
<span class="js-op">}</span></code></pre></div><p>When <code>i</code> becomes 3, the loop repeatedly executes <code>continue</code> without increasing <code>i</code>. This creates an infinite loop.</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-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-id">i</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-id">i</span><span class="js-op">++;</span>
<span class="js-op">}</span></code></pre></div><h3>4. Using goto Without a Clear Reason</h3><p>Excessive use of <code>goto</code> can make a program difficult to read and maintain.</p><p>Prefer <code>for</code>, <code>while</code>, <code>if</code>, and function-based solutions.</p><h3>5. Returning the Wrong Data Type</h3><p>A function declared to return an integer should return a compatible integer value.</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">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></code></pre></div><p>Make sure the returned value matches the function's declared return type.</p><h3>6. Forgetting That break Only Exits the Nearest Loop</h3><p>When using nested loops, <code>break</code> terminates only the innermost loop that contains it.</p><p>If you want to stop multiple nested loops, you need additional control logic, a function return, or another appropriate structured solution.</p><h2>Best Practices for Using Jump Statements in C++</h2><p>Follow these simple practices when writing C++ programs:</p><ul><li><p>Use <code>break</code> when you need to terminate a loop or switch.</p></li><li><p>Use <code>continue</code> when you want to skip a particular iteration.</p></li><li><p>Use <code>return</code> to finish a function or send a result back.</p></li><li><p>Avoid unnecessary use of <code>goto</code>.</p></li><li><p>Always make sure loop variables are updated correctly.</p></li><li><p>Use meaningful conditions with jump statements.</p></li><li><p>Be careful when using jump statements inside nested loops.</p></li><li><p>Keep functions small and easy to understand.</p></li><li><p>Return values that match the declared function type.</p></li><li><p>Test different conditions to verify program behaviour.</p></li></ul><h2>Frequently Asked Questions About Jump Statements in C++</h2><h3>1. What are jump statements in C++?</h3><p>Jump statements are instructions that change the normal flow of execution in a C++ program. The four main jump statements are <code>break</code>, <code>continue</code>, <code>goto</code>, and <code>return</code>.</p><h3>2. How many jump statements are there in C++?</h3><p>C++ has four main jump statements: <code>break</code>, <code>continue</code>, <code>goto</code>, and <code>return</code>.</p><h3>3. What is the use of break in C++?</h3><p>The <code>break</code> statement immediately terminates the nearest enclosing loop or <code>switch</code> statement.</p><h3>4. What is the use of continue in C++?</h3><p>The <code>continue</code> statement skips the remaining statements in the current loop iteration and moves to the next iteration.</p><h3>5. What is the use of goto in C++?</h3><p>The <code>goto</code> statement transfers program execution to a labeled statement within the same function.</p><h3>6. What is the use of return in C++?</h3><p>The <code>return</code> statement terminates a function and can return a value to the calling function.</p><h3>7. What is the difference between break and continue?</h3><p><code>break</code> terminates the entire nearest enclosing loop, whereas <code>continue</code> skips only the current iteration.</p><h3>8. Can break be used outside a loop in C++?</h3><p>Yes. The <code>break</code> statement can also be used inside a <code>switch</code> statement. It cannot be used freely in an ordinary block or function.</p><h3>9. Can continue be used in a switch statement?</h3><p>A <code>continue</code> statement can appear inside a <code>switch</code> nested within a loop. It applies to the nearest enclosing loop, not the switch statement itself.</p><h3>10. Is goto recommended in C++?</h3><p>Generally, no. Although C++ supports <code>goto</code>, structured alternatives such as loops, functions, and conditional statements usually produce more readable code.</p><h3>11. Can return be used inside a loop?</h3><p>Yes. A <code>return</code> statement can be used inside a loop to terminate the entire function, not just the loop.</p><h3>12. What happens when return 0 is used in main()?</h3><p><code>return 0;</code> normally indicates that the program completed successfully and sends a success status to the operating system.</p><h3>13. What is the difference between break and return?</h3><p><code>break</code> exits the nearest enclosing loop or switch, while <code>return</code> exits the entire function.</p><h3>14. Can goto jump outside a function?</h3><p>No. A <code>goto</code> statement cannot transfer control to a label in another function. Its destination must be within the same function.</p>
Code Example
#include <iostream>
struct ResourceTracker {
std::string name;
ResourceTracker(std::string n) : name(n) { std::cout << " Constructed: " << name << "
"; }
~ResourceTracker() { std::cout << " Destructed: " << name << "
"; }
};
int searchNumber(int target) {
ResourceTracker tracker("SearchScope");
for (int i = 1; i <= 5; ++i) {
if (i == target) {
std::cout << "Target found! Returning from function.
";
return i; // Automatically destructs 'tracker' before returning
}
}
return -1;
}
int main() {
std::cout << "Starting search...
";
int result = searchNumber(3);
std::cout << "Search result: " << result << "
";
return 0;
}Interview Question & Tips
A: Yes. ISO C++ guarantees deterministic stack unwinding: before control exits the function, all local automatic objects are destroyed in reverse order of their creation.
Q: When is goto acceptable in modern C++?
A: In modern C++, `goto` is rarely needed due to exceptions, break/continue, and structured programming. Occasionally, it is used in low-level state machines or breaking out of deeply nested multi-level loops.
Best Practices
2. Ensure every non-void function path explicitly returns a valid value.
3. Leverage early returns (guard clauses) to simplify deep conditional nesting.
This study reference for Jump Statements in C++: break, continue, goto, and return is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: