C++ Programming 3. Control Flow in C++ 10 mins read 16,105 views

The continue Statement in C++: Skipping Selected Iterations

The continue statement in C++ is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when you want...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is The continue Statement in C++: Skipping Selected Iterations? The continue statement in C++ is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when you want...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How does continue skip the remainder of the current iteration without terminating the loop?
T<span style="color: rgb(51, 65, 85); font-size: 0.95rem;">he </span><code dir="ltr">continue</code><span style="color: rgb(51, 65, 85); font-size: 0.95rem;"> statement in C++ is used to skip the remaining statements in the current loop iteration and move to the next iteration. It is useful when you want a loop to ignore specific values or conditions without stopping the entire loop.</span><div><font color="#334155"><br></font><p>For example, suppose you want to print numbers from 1 to 10 but skip the number 5. Instead of ending the loop, you can use the <code dir="ltr">continue</code> statement to skip that particular iteration.</p></div>

Explanation & In-Depth Details

<h2 class="h4 fw-bold text-dark mb-3"><span style="font-size: 1.15rem;">1. What Is the continue Statement in C++?</span></h2><p>The <code>continue</code> statement is a jump statement that skips the remaining statements in the current iteration of a loop.</p><p>When C++ encounters <code>continue</code>:</p><ol start="1"><li><p>The remaining statements in the current iteration are skipped.</p></li><li><p>The loop moves towards its next iteration.</p></li><li><p>The loop's condition is checked again when required.</p></li></ol><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">continue</span><span class="js-op">;</span></code></pre></div><p>The <code>continue</code> statement can be used inside:</p><ul><li><p><code>for</code> loops</p></li><li><p><code>while</code> loops</p></li><li><p><code>do-while</code> loops</p></li><li><p>Range-based <code>for</code> loops</p></li></ul><p>It cannot be used in a <code>switch</code> statement by itself because <code>switch</code> is not a loop.</p><h2>2. Simple Example of continue in C++</h2><p>Let's start with a simple example that prints numbers from 1 to 5 but skips the number 3.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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><strong>Explanation:</strong></p><ul><li><p>The loop starts with <code>i = 1</code>.</p></li><li><p>The program prints 1 and 2.</p></li><li><p>When <code>i</code> becomes 3, the condition <code>i == 3</code> becomes true.</p></li><li><p>The <code>continue</code> statement skips the <code>cout</code> statement for that iteration.</p></li><li><p>The loop continues with 4 and 5.</p></li></ul><p>The important point is that the loop does not terminate when <code>continue</code> executes.</p><h2>3. Using continue in a for Loop</h2><p>A <code>for</code> loop is commonly used when you know how many times a task needs to be repeated.</p><p>The <code>continue</code> statement skips the remaining statements in the current iteration. In a traditional <code>for</code> loop, execution then moves to the update expression before checking the loop condition again.</p><h3>Example: Skip Even Numbers</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">3</span> <span class="js-num">5</span> <span class="js-num">7</span> <span class="js-num">9</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The loop runs from 1 to 10.</p></li><li><p>The condition <code>i % 2 == 0</code> checks whether a number is even.</p></li><li><p>If the number is even, <code>continue</code> skips the print statement.</p></li><li><p>Only odd numbers are displayed.</p></li></ul><h3>Example: Skip a Specific Number</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</span> <span class="js-num">7</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">continue</span><span class="js-op">;</span>
<span class="js-op">}</span>

<span class="js-id">cout</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">"Number: "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</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">Number</span><span class="js-op">:</span> <span class="js-num">1</span>
<span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">2</span>
<span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">3</span>
<span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">5</span>
<span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">6</span>
<span class="js-id">Number</span><span class="js-op">:</span> <span class="js-num">7</span></code></pre></div><p>Here, the loop skips 4 but continues processing all the other numbers.</p><h2>4. Using continue in a while Loop</h2><p>A <code>while</code> loop executes as long as its condition remains true.</p><p>When <code>continue</code> is executed inside a <code>while</code> loop, the remaining statements in the current iteration are skipped, and the loop condition is checked again.</p><h3>Example: Skip Number 5</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</span> <span class="js-num">7</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-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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">6</span> <span class="js-num">7</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The variable <code>i</code> starts at 1.</p></li><li><p>The loop prints numbers until it reaches 5.</p></li><li><p>When <code>i</code> is 5, the program increments <code>i</code> and executes <code>continue</code>.</p></li><li><p>The print statement is skipped for that iteration.</p></li><li><p>The loop continues with 6 and 7.</p></li></ul><p><strong>Important:</strong> In a <code>while</code> loop, make sure your loop variable is updated before executing <code>continue</code> when the update would otherwise be skipped. Forgetting this can create an infinite loop.</p><h2>5. Using continue in a do-while Loop</h2><p>A <code>do-while</code> loop executes its body at least once before checking the loop condition.</p><p>When <code>continue</code> executes inside a <code>do-while</code> loop, control moves to the condition check at the end of the loop.</p><h3>Example: Skip Even Numbers</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">do</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">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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">&lt;</span> <span class="js-num">10</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">3</span> <span class="js-num">5</span> <span class="js-num">7</span> <span class="js-num">9</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The variable <code>i</code> starts at 0.</p></li><li><p>It is incremented at the beginning of each iteration.</p></li><li><p>If the number is even, <code>continue</code> skips the print statement.</p></li><li><p>The loop condition is checked.</p></li><li><p>The loop continues until <code>i</code> reaches 10.</p></li></ul><h2>6. Using continue in a Range-Based for Loop</h2><p>A range-based <code>for</code> loop is used to process each element in an array, container, or other suitable range.</p><p>You can use <code>continue</code> to skip selected elements.</p><h3>Example: Skip a Specific Array Element</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">numbers</span>[] <span class="js-op">=</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-num">30</span><span class="js-op">,</span> <span class="js-num">40</span><span class="js-op">,</span> <span class="js-num">50</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">number</span> <span class="js-op">:</span> <span class="js-id">numbers</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">30</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">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">10</span> <span class="js-num">20</span> <span class="js-num">40</span> <span class="js-num">50</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The range-based loop processes each array element.</p></li><li><p>When the value is 30, <code>continue</code> skips the print statement.</p></li><li><p>The next element is processed.</p></li><li><p>All other values are printed.</p></li></ul><h2>7. Practical Example: Print Only Positive Numbers</h2><p>Suppose you have an array containing positive, negative, and zero values. You want to display only positive numbers.</p><p>You can use <code>continue</code> to skip values that are zero or negative.</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">numbers</span>[] <span class="js-op">=</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-num">5</span><span class="js-op">,</span> <span class="js-num">20</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-num">8</span><span class="js-op">,</span> <span class="js-num">30</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">number</span> <span class="js-op">:</span> <span class="js-id">numbers</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">&lt;=</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">&lt;&lt;</span> <span class="js-id">number</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">10</span> <span class="js-num">20</span> <span class="js-num">30</span></code></pre></div><p><strong>Explanation:</strong></p><p>The condition <code>number &lt;= 0</code> identifies zero and negative values. The <code>continue</code> statement skips those elements, so only positive numbers are displayed.</p><h2>8. Using continue in Nested Loops</h2><p>A nested loop means one loop is placed inside another loop.</p><p>When <code>continue</code> is used inside an inner loop, it skips the current iteration of that inner loop. The outer loop continues normally.</p><h3>Example: Skip One Value in an Inner 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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">&lt;=</span> <span class="js-num">4</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">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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</span> <span class="js-op">&lt;&lt;</span> <span class="js-id">j</span> <span class="js-op">&lt;&lt;</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">2</span>
<span class="js-num">1</span> <span class="js-num">4</span>
<span class="js-num">2</span> <span class="js-num">1</span>
<span class="js-num">2</span> <span class="js-num">2</span>
<span class="js-num">2</span> <span class="js-num">4</span>
<span class="js-num">3</span> <span class="js-num">1</span>
<span class="js-num">3</span> <span class="js-num">2</span>
<span class="js-num">3</span> <span class="js-num">4</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The outer loop runs three times.</p></li><li><p>The inner loop runs from 1 to 4 during each outer iteration.</p></li><li><p>When <code>j</code> becomes 3, <code>continue</code> skips the current inner-loop iteration.</p></li><li><p>The inner loop proceeds to 4.</p></li><li><p>The outer loop continues normally.</p></li></ul><h2>9. Using continue Inside a switch Nested in a Loop</h2><p>A <code>continue</code> statement cannot be used in a standalone <code>switch</code> statement. However, it can be used inside a <code>switch</code> that is itself inside a loop.</p><p>In that situation, <code>continue</code> applies to the nearest enclosing loop, not the <code>switch</code>.</p><h3>Example: Skip a Value Using switch</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">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">switch</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">case</span> <span class="js-num">3</span><span class="js-op">:</span>
<span class="js-kw">continue</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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">2</span> <span class="js-num">4</span> <span class="js-num">5</span></code></pre></div><p><strong>Explanation:</strong></p><ul><li><p>The <code>for</code> loop processes numbers from 1 to 5.</p></li><li><p>The <code>switch</code> checks the current value.</p></li><li><p>When <code>i</code> is 3, <code>continue</code> skips the rest of the current loop iteration.</p></li><li><p>The loop moves to the next value.</p></li></ul><h2>10. Difference Between break and continue in C++</h2><p>Both <code>break</code> and <code>continue</code> are jump statements, but they control loops differently.</p><table><tbody><tr><th>Feature</th><th>break</th><th>continue</th></tr><tr><td>Purpose</td><td>Terminates the nearest enclosing loop or switch</td><td>Skips the remaining statements in the current loop iteration</td></tr><tr><td>Loop execution</td><td>Stops the loop</td><td>Continues with the next iteration</td></tr><tr><td>Used in for loop</td><td>Yes</td><td>Yes</td></tr><tr><td>Used in while loop</td><td>Yes</td><td>Yes</td></tr><tr><td>Used in do-while loop</td><td>Yes</td><td>Yes</td></tr><tr><td>Used in switch alone</td><td>Yes</td><td>No</td></tr><tr><td>Typical use</td><td>Stop searching after finding a value</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-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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></code></pre></div><p>The loop stops completely when <code>i</code> becomes 3.</p><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-id">include</span> <span class="js-op">&lt;</span><span class="js-id">iostream</span><span class="js-op">&gt;</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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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 loop skips 3 but continues processing the remaining values.</p><h2>11. Common Mistakes When Using continue</h2><h3>Mistake 1: Forgetting to Update a while Loop Variable</h3><p>Incorrect 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">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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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><strong>Problem:</strong> When <code>i</code> becomes 3, the <code>continue</code> statement runs before <code>i++</code>. The value remains 3, so the condition stays true and the loop never ends.</p><p><strong>Correct 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">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">&lt;=</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">&lt;&lt;</span> <span class="js-id">i</span> <span class="js-op">&lt;&lt;</span> <span class="js-str">" "</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><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>Mistake 2: Expecting continue to Exit the Loop</h3><p>The <code>continue</code> statement does not terminate the loop. It only skips the current iteration.</p><p>Use <code>break</code> if you want to stop the loop completely.</p><h3>Mistake 3: Using continue Outside a Loop</h3><p>Incorrect 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">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-kw">continue</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>This code produces a compilation error because <code>continue</code> is not inside a loop.</p><h3>Mistake 4: Expecting continue to Skip an Entire switch Statement</h3><p>The <code>continue</code> statement does not directly terminate a <code>switch</code> block. When a <code>switch</code> is inside a loop, <code>continue</code> skips the current iteration of that enclosing loop.</p><h2>12. Best Practices for Using continue in C++</h2><p>Follow these practices when using <code>continue</code>:</p><ol start="1"><li><p>Use <code>continue</code> when you want to skip unwanted values without stopping the loop.</p></li><li><p>Keep the condition before the statements you want to skip.</p></li><li><p>In <code>while</code> loops, ensure the loop variable or other progress condition is updated before a possible <code>continue</code>.</p></li><li><p>In nested loops, remember that <code>continue</code> applies to the nearest enclosing loop.</p></li><li><p>Avoid using too many <code>continue</code> statements if they make the program difficult to understand.</p></li><li><p>Use meaningful conditions so other developers can easily understand why an iteration is skipped.</p></li><li><p>Test boundary values and conditions that might cause an infinite loop.</p></li></ol><h2>13. Frequently Asked Questions (FAQs)</h2><h3>Q1. What is the continue statement in C++?</h3><p>The <code>continue</code> statement skips the remaining statements in the current iteration of a loop and moves execution towards the next iteration.</p><h3>Q2. Can we use continue in a for loop?</h3><p>Yes. You can use <code>continue</code> inside a <code>for</code> loop to skip selected iterations. The loop's update expression still executes before the next condition check.</p><h3>Q3. Can we use continue in a while loop?</h3><p>Yes. In a <code>while</code> loop, <code>continue</code> skips the remaining statements and moves control to the next condition check.</p><h3>Q4. Can continue be used in a do-while loop?</h3><p>Yes. In a <code>do-while</code> loop, <code>continue</code> skips the remaining statements and moves control to the condition check at the end of the loop.</p><h3>Q5. Can we use continue in a switch statement?</h3><p>No, not in a standalone <code>switch</code> statement. However, if the <code>switch</code> is inside a loop, <code>continue</code> can be used to continue the enclosing loop.</p><h3>Q6. What is the difference between break and continue?</h3><p><code>break</code> terminates the nearest enclosing loop or <code>switch</code>, whereas <code>continue</code> skips the current loop iteration and proceeds with the next one.</p><h3>Q7. Does continue work in nested loops?</h3><p>Yes. It skips the current iteration of the nearest enclosing loop. Other enclosing loops continue normally.</p><h3>Q8. Does continue increase the loop variable automatically?</h3><p>Not always. In a traditional <code>for</code> loop, the update expression runs after <code>continue</code>. In <code>while</code> and <code>do-while</code> loops, you must ensure your code updates the loop variable where necessary.</p>

Code Example

#include <iostream>

int main() {
    std::cout << "Printing odd numbers between 1 and 10:
";
    for (int i = 1; i <= 10; ++i) {
        // Skip even numbers
        if (i % 2 == 0) {
            continue; // Skip the rest of the loop body
        }
        std::cout << i << " ";
    }
    std::cout << "
";
    return 0;
}

Interview Question & Tips

Q: What is the key behavioral difference between continue in a for loop vs a while loop?
A: In a `for` loop, `continue` jumps to the increment expression (`++i`) before checking the condition. In a `while` loop, it jumps directly to the condition; if the counter is updated at the bottom of the while body, an infinite loop can occur if not handled carefully.

Best Practices

1. Use `continue` as an early guard check at the top of loops to avoid deeply nested `if` statements.
2. Ensure loop update variables in `while` loops are incremented before invoking `continue` to avoid infinite loops.
Academic Standards & Verified Citations

This study reference for The continue Statement in C++: Skipping Selected Iterations is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming