C Programming Control Flow in C 11 mins read 19,706 views

C do...while Loop: Execute First, Check the Condition Later

Every loop you've met so far is cautious. for and while check their conditions before daring to run the body — and if the conditio...
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 C do...while Loop: Execute First, Check the Condition Later? Every loop you've met so far is cautious. for and while check their conditions before daring to run the body — and if the conditio...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C Programming.
How does the `do...while` loop ensure at least one execution of the loop body, and why is the trailing semicolon required?
<p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Every loop you've met so far is cautious.&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">for</code>&nbsp;and&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;check their conditions&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">before</em>&nbsp;daring to run the body — and if the condition fails at the door, the body never executes at all. Cautious is usually smart.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">But sometimes caution is the wrong shape for the problem. Think about a menu: you can't ask the user to choose before you've&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">shown</em>&nbsp;them the menu. A password prompt: you can't check the input before you've&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">received</em>&nbsp;it. Some code simply&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">must run once</span>&nbsp;before any condition makes sense.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">That's the entire reason&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;exists. It's C's optimist:&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">execute first, ask questions later.</em>&nbsp;The body always runs at least once — guaranteed — and only then does the condition decide whether to go again.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: the hero flowchart — Body FIRST, then condition diamond below it, "true" loops back up to the body, "false" exits. Visually mirror this against the while-loop flowchart from the previous page.)</em></p>

Explanation & In-Depth Details

<h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">The Syntax</h2><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-cm">// body — ALWAYS runs at least once</span>
<span class="js-op">}</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">condition</span><span class="js-op">);</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Three details to notice:</p><ol class="list-inside list-decimal whitespace-normal [li_&amp;]:pl-6" data-streamdown="ordered-list" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); list-style-position: outside; list-style-image: initial; padding-inline-start: 1.375rem; margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><li class="py-1 [&amp;&gt;p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding-top: 0px; padding-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;">The body comes&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">first</span>, wrapped in braces after&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do</code></li><li class="py-1 [&amp;&gt;p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding-top: 0px; padding-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;">The condition comes&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">after</span>, at the bottom</li><li class="py-1 [&amp;&gt;p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding-top: 0px; padding-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;">There's a&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">semicolon after&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while (condition);</code></span>&nbsp;— required, and forgetting it is one of the most common compile errors beginners hit with this loop</li></ol><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">A first example:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">do</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"%d "</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-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-cm">// Output: 1 2 3 4 5</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Looks familiar so far. But now watch the difference that defines this loop...</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">The Guarantee in Action: One Run, No Matter What</h2><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">x</span> <span class="js-op">=</span> <span class="js-num">100</span><span class="js-op">;</span>
<span class="js-cm">// while version — prints NOTHING:while (x &lt; 10) { printf("while: x = %d\n", x); x++;}</span>
<span class="js-cm">// do-while version — prints ONCE despite the false condition:x = 100;do { printf("do-while: x = %d\n", x); x++;} while (x &lt; 10);</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Output:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">do</span><span class="js-op">-</span><span class="js-kw">while</span><span class="js-op">:</span> <span class="js-id">x</span> <span class="js-op">=</span> <span class="js-num">100</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">The&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;loop checked first, found&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">100 &lt; 10</code>&nbsp;false, and skipped its body entirely. The&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;ran its body&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">before ever checking</em>&nbsp;— so it printed once, then saw the same false condition and stopped. That single guaranteed execution is the whole personality of this loop.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">This is why you'll hear them described as:</p><ul class="list-inside list-disc whitespace-normal [li_&amp;]:pl-6" data-streamdown="unordered-list" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); list-style-position: outside; list-style-image: initial; padding-inline-start: 1.375rem; margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><li class="py-1 [&amp;&gt;p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding-top: 0px; padding-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;= entry-controlled loop</span>&nbsp;(check at the door)</li><li class="py-1 [&amp;&gt;p]:inline" data-streamdown="list-item" dir="ltr" style="border: 0px solid rgb(47, 47, 55); padding-top: 0px; padding-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-block: 0.125rem;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;= exit-controlled loop</span>&nbsp;(check on the way out)</li></ul><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: side-by-side flowchart — "while: gatekeeper BEFORE the room" vs "do-while: gatekeeper AFTER the room")</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">The Killer Application: Menu-Driven Programs</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Ask any C instructor where&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;truly earns its keep, and the answer is unanimous:&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">menus</span>. Show the menu, take the choice, act on it, repeat until the user quits. The menu must appear at least once — the loop's guarantee fits the problem like a glove:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;">#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">stdio</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">&gt;</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-kw">do</span> <span class="js-op">{</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"\n===== MAIN MENU ====="</span><span class="js-op">);</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"1. Check balance"</span><span class="js-op">);</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"2. Deposit"</span><span class="js-op">);</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"3. Withdraw"</span><span class="js-op">);</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"0. Exit"</span><span class="js-op">);</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Your choice: "</span><span class="js-op">);</span> <span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">"%d"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">choice</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">puts</span><span class="js-op">(</span><span class="js-str">"Balance: ₹5,000.00"</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">puts</span><span class="js-op">(</span><span class="js-str">"Deposit feature coming!"</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">puts</span><span class="js-op">(</span><span class="js-str">"Withdraw feature coming!"</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">0</span><span class="js-op">:</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Goodbye!"</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">puts</span><span class="js-op">(</span><span class="js-str">"Invalid option. Try again."</span><span class="js-op">);</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">choice</span> <span class="js-op">!=</span> <span class="js-num">0</span><span class="js-op">);</span>
<span class="js-kw">return</span> <span class="js-num">0</span><span class="js-op">;}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample Run:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-op">=====</span> <span class="js-id">MAIN</span> <span class="js-id">MENU</span> <span class="js-op">=====</span><span class="js-num">1</span><span class="js-op">.</span> <span class="js-id">Check</span> <span class="js-id">balance2</span><span class="js-op">.</span> <span class="js-id">Deposit3</span><span class="js-op">.</span> <span class="js-id">Withdraw0</span><span class="js-op">.</span> <span class="js-id">ExitYour</span> <span class="js-id">choice</span><span class="js-op">:</span> 1Balance<span class="js-op">:</span> ₹<span class="js-num">5</span><span class="js-op">,</span><span class="js-num">000.00</span>
<span class="js-op">=====</span> <span class="js-id">MAIN</span> <span class="js-id">MENU</span> <span class="js-op">=====...</span><span class="js-id">Your</span> <span class="js-id">choice</span><span class="js-op">:</span> 0Goodbye<span class="js-op">!</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Look how naturally the pieces fit:&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;guarantees the menu displays,&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">switch</code>&nbsp;handles the choice (remember our switch tutorial — one variable, exact values), and the loop repeats until the sentinel&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">0</code>&nbsp;appears.&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Menu + do-while + switch</span>&nbsp;is a combination you'll see in thousands of student projects and real console applications.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Try writing this with a plain&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;and you'll immediately feel the friction — you'd have to show the menu once&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">before</em>&nbsp;the loop (duplicated code) or prime&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">choice</code>&nbsp;with a fake value (a hack).&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;says exactly what you mean.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">Input Validation: The Natural Second Home</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Remember the validation loop from the&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;tutorial? It needed a "priming read" — the prompt written twice:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-cm">// while version — prompt appears TWICE:printf("Enter age (1-120): ");scanf("%d", &amp;age);</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">&lt;</span> <span class="js-num">1</span> <span class="js-op">||</span> <span class="js-id">age</span> <span class="js-op">&gt;</span> <span class="js-num">120</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Invalid! Try again: "</span><span class="js-op">);</span> <span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">"%d"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">age</span><span class="js-op">);}</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;absorbs the first attempt into the loop:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-cm">// do-while version — prompt written ONCE:int age;</span>
<span class="js-kw">do</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Enter age (1-120): "</span><span class="js-op">);</span> <span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">"%d"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">age</span><span class="js-op">);</span>
<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">age</span> <span class="js-op">&lt;</span> <span class="js-num">1</span> <span class="js-op">||</span> <span class="js-id">age</span> <span class="js-op">&gt;</span> <span class="js-num">120</span><span class="js-op">)</span> <span class="js-op">{</span> <span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Invalid! Please try again."</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">age</span> <span class="js-op">&lt;</span> <span class="js-num">1</span> <span class="js-op">||</span> <span class="js-id">age</span> <span class="js-op">&gt;</span> <span class="js-num">120</span><span class="js-op">);</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Age recorded: %d\n"</span><span class="js-op">,</span> <span class="js-id">age</span><span class="js-op">);</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample Run:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">Enter</span> <span class="js-id">age</span> <span class="js-op">(</span><span class="js-num">1</span><span class="js-op">-</span><span class="js-num">120</span><span class="js-op">):</span> 500Invalid<span class="js-op">!</span> <span class="js-id">Please</span> <span class="js-kw">try</span> <span class="js-id">again</span><span class="js-op">.</span><span class="js-id">Enter</span> <span class="js-id">age</span> <span class="js-op">(</span><span class="js-num">1</span><span class="js-op">-</span><span class="js-num">120</span><span class="js-op">):</span> <span class="js-op">-</span>3Invalid<span class="js-op">!</span> <span class="js-id">Please</span> <span class="js-kw">try</span> <span class="js-id">again</span><span class="js-op">.</span><span class="js-id">Enter</span> <span class="js-id">age</span> <span class="js-op">(</span><span class="js-num">1</span><span class="js-op">-</span><span class="js-num">120</span><span class="js-op">):</span> 28Age <span class="js-id">recorded</span><span class="js-op">:</span> <span class="js-num">28</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">One prompt, one loop, no duplication. Whenever "ask at least once, repeat if wrong" describes your problem,&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;is the idiomatic C answer.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">do-while vs while vs for: The Final Verdict</h2><div class="my-4 flex flex-col gap-2 rounded-lg bg-sidebar p-2" data-streamdown="table-wrapper" style="border-width: 0.8px; border-style: solid; border-color: rgb(47, 47, 55); border-image: none 100% / 1 / 0 stretch; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 0.5rem; margin-block: 1rem 0.75rem; overflow: hidden; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); gap: 0px; --tw-space-y-reverse: 0; display: flex; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px;"><div class="border-collapse" style="border: 1px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); border-collapse: collapse; border-radius: 5.5px; background-color: rgb(18, 18, 22); max-height: 300px;"><table class="w-full divide-y divide-border" data-streamdown="table" style="border-width: 0px; border-color: inherit; margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); text-indent: 0px; min-width: 100%; width: 956.8px;"><thead class="bg-muted/80" data-streamdown="table-header" style="border-bottom-width: 0.8px; border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0; background-color: oklab(0.252138 0.000131465 -0.00581433 / 0.8);"><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; text-wrap-mode: nowrap;"></th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; text-wrap-mode: nowrap;">while</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; text-wrap-mode: nowrap;">do-while</th><th class="whitespace-nowrap px-4 py-2 text-left font-semibold text-sm" data-streamdown="table-header-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px; --tw-font-weight: 600; text-wrap-mode: nowrap;">for</th></tr></thead><tbody class="divide-y divide-border" data-streamdown="table-body" style="border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><tr class="" data-streamdown="table-row" style="border-bottom-width: 0.8px; border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Condition checked</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Before body</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">After body</span></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Before body</td></tr><tr class="" data-streamdown="table-row" style="border-bottom-width: 0.8px; border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Minimum executions</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">0</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">1 (guaranteed)</span></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">0</td></tr><tr class="" data-streamdown="table-row" style="border-bottom-width: 0.8px; border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Best for</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Condition-driven, might skip entirely</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Must-run-once: menus, prompts, validation</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Counted repetition</td></tr><tr class="" data-streamdown="table-row" style="border-bottom-width: 0.8px; border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-divide-y-reverse: 0;"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Semicolon after condition</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">No</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Yes — required!</span></td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">n/a (header)</td></tr><tr class="" data-streamdown="table-row" style="border-color: rgb(47, 47, 55); border-image: initial; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);"><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Loop family name</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Entry-controlled</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Exit-controlled</td><td class="px-4 py-2 text-sm" data-streamdown="table-cell" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); padding-inline: 15px; padding-block: 7.5px; font-size: 13px; line-height: 18.75px;">Entry-controlled</td></tr></tbody></table></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">The decision question is one sentence long:</span>&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">"Does this code need to run even when the condition is already false?"</em>&nbsp;Yes →&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>. No →&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>. Counting →&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">for</code>.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">The Classic Mistakes</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">1. Forgetting the semicolon</span>&nbsp;(the signature error of this loop):</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">do</span> <span class="js-op">{</span> <span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Hello\n"</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-cm">// ✗ compile error — missing semicolon!</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-cm">// ✓ correct</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">After a&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;loop, no semicolon; after&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>, mandatory. Your muscle memory from&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;will betray you here — everyone gets caught once.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">2. Using do-while when zero executions are required:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-cm">// DANGER: body runs even for an empty list!do {</span>
<span class="js-id">process</span><span class="js-op">(</span><span class="js-id">item</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">moreItems</span><span class="js-op">());</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">If the list might be empty, "always run once" becomes a bug, not a feature. That's a&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;job.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">3. Declaring a variable inside the body, then using it in the condition:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">do</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-cm">// declared INSIDE scanf("%d", &amp;choice);</span>
<span class="js-op">}</span>
<span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">choice</span> <span class="js-op">!=</span> <span class="js-num">0</span><span class="js-op">);</span> <span class="js-cm">// ✗ ERROR — choice is out of scope here!</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Variables declared in the body die at the closing brace —&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">before</em>&nbsp;the condition is checked. Declare them&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">before</span>&nbsp;the&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do</code>:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">choice</span><span class="js-op">;</span> <span class="js-cm">// ✓ outside the loop</span>
<span class="js-kw">do</span> <span class="js-op">{</span> <span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">"%d"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">choice</span><span class="js-op">);}</span> <span class="js-kw">while</span> <span class="js-op">(</span><span class="js-id">choice</span> <span class="js-op">!=</span> <span class="js-num">0</span><span class="js-op">);</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">This one surprises even experienced programmers switching from other languages.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">4. The missing update</span>&nbsp;— same infinite-loop trap as&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-id">int</span> <span class="js-id">i</span> <span class="js-op">=</span> <span class="js-num">1</span><span class="js-op">;</span>
<span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"%d "</span><span class="js-op">,</span> <span class="js-id">i</span><span class="js-op">);</span>
<span class="js-cm">// forgot i++ — "1 1 1 1..." forever</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></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">5. The stray semicolon after do:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-kw">do</span><span class="js-op">;</span> <span class="js-cm">// ← semicolon makes an empty statement</span>
<span class="js-op">{</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Hello\n"</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">5</span><span class="js-op">);</span> <span class="js-cm">// confusing errors follow</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">(Suggested image: "5 do-while Mistakes" checklist graphic — consistent with the series)</em></p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">A Complete Example: The Number Guessing Game, Revisited</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Remember the guessing game from the Control Flow pillar? Here's a cleaner version that uses&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;plus a "play again" feature — the second place this loop shines:</p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="c" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="language-c" data-language="c" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="language-c bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;">#<span class="js-id">include</span> <span class="js-op">&lt;</span><span class="js-id">stdio</span><span class="js-op">.</span><span class="js-id">h</span><span class="js-op">&gt;</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">char</span> <span class="js-id">playAgain</span><span class="js-op">;</span>


<span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-id">int</span> <span class="js-id">secret</span> <span class="js-op">=</span> <span class="js-num">42</span><span class="js-op">,</span> <span class="js-id">guess</span><span class="js-op">,</span> <span class="js-id">attempts</span> <span class="js-op">=</span> <span class="js-num">0</span><span class="js-op">;</span>


<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"\n=== Guess the number (1-100) ==="</span><span class="js-op">);</span>


<span class="js-kw">do</span> <span class="js-op">{</span>
<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Your guess: "</span><span class="js-op">);</span>
<span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">"%d"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">guess</span><span class="js-op">);</span>
<span class="js-id">attempts</span><span class="js-op">++;</span>


<span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">guess</span> <span class="js-op">&lt;</span> <span class="js-id">secret</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Too low!"</span><span class="js-op">);</span>
<span class="js-op">}</span> <span class="js-kw">else</span> <span class="js-kw">if</span> <span class="js-op">(</span><span class="js-id">guess</span> <span class="js-op">&gt;</span> <span class="js-id">secret</span><span class="js-op">)</span> <span class="js-op">{</span>
<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Too high!"</span><span class="js-op">);</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">guess</span> <span class="js-op">!=</span> <span class="js-id">secret</span><span class="js-op">);</span>


<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Correct! It took %d attempts.\n"</span><span class="js-op">,</span> <span class="js-id">attempts</span><span class="js-op">);</span>


<span class="js-id">printf</span><span class="js-op">(</span><span class="js-str">"Play again? (y/n): "</span><span class="js-op">);</span>
<span class="js-id">scanf</span><span class="js-op">(</span><span class="js-str">" %c"</span><span class="js-op">,</span> <span class="js-op">&amp;</span><span class="js-id">playAgain</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">playAgain</span> <span class="js-op">==</span> <span class="js-str">'y'</span> <span class="js-op">||</span> <span class="js-id">playAgain</span> <span class="js-op">==</span> <span class="js-str">'Y'</span><span class="js-op">);</span>


<span class="js-id">puts</span><span class="js-op">(</span><span class="js-str">"Thanks for playing!"</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></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Sample Run:</span></p><div class="my-4 flex w-full flex-col gap-2 rounded-xl bg-sidebar p-2" data-language="" data-streamdown="code-block" dir="ltr" style="border: medium none currentcolor; margin-top: 0px; margin-bottom: 0px; padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); max-width: 100%; border-radius: 7.5px; margin-block: 1rem 0.75rem; background: 0px 0px rgb(18, 18, 22); gap: 0px; --tw-space-y-reverse: 0; display: flex; width: 960px; flex-direction: column; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; content-visibility: auto; contain-intrinsic-size: auto 200px;"><div class="" data-language="" data-streamdown="code-block-body" dir="ltr" style="border: 1px solid rgb(47, 47, 55); padding: 0.625rem 0.75rem; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(24, 25, 27); border-radius: 0.5rem; font-size: 0.8125rem; line-height: 1.6; max-height: 400px;"><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/c-compiler" target="_blank" class="btn-code-action run-code-btn" title="Run Online">
<i class="fas fa-play"></i>
</a>
</div>
<pre class="bg-[var(--sdm-bg,inherit)] dark:bg-[var(--shiki-dark-bg,var(--sdm-bg,inherit))] code-box" style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); padding: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text; background-position: 0px 0px; background-color: initial; --sdm-bg: transparent; --sdm-fg: inherit;"><code style="border-width: 0px; border-style: solid; border-color: rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 1em; user-select: text;"><span class="js-op">===</span> <span class="js-id">Guess</span> <span class="js-id">the</span> <span class="js-id">number</span> <span class="js-op">(</span><span class="js-num">1</span><span class="js-op">-</span><span class="js-num">100</span><span class="js-op">)</span> <span class="js-op">===</span>
<span class="js-id">Your</span> <span class="js-id">guess</span><span class="js-op">:</span> <span class="js-num">50</span>
<span class="js-id">Too</span> <span class="js-id">high</span><span class="js-op">!</span>
<span class="js-id">Your</span> <span class="js-id">guess</span><span class="js-op">:</span> <span class="js-num">42</span>
<span class="js-id">Correct</span><span class="js-op">!</span> <span class="js-id">It</span> <span class="js-id">took</span> <span class="js-num">2</span> <span class="js-id">attempts</span><span class="js-op">.</span>
<span class="js-id">Play</span> <span class="js-id">again</span><span class="js-op">?</span> <span class="js-op">(</span><span class="js-id">y</span><span class="js-op">/</span><span class="js-id">n</span><span class="js-op">):</span> <span class="js-id">n</span>
<span class="js-id">Thanks</span> <span class="js-kw">for</span> <span class="js-id">playing</span><span class="js-op">!</span></code></pre></div></div></div><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">Two nested do-while loops</span>, each doing its signature job: the inner one guarantees at least one guess, the outer one guarantees at least one game. Plus the space in&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">scanf(" %c")</code>&nbsp;from our scanf tutorial handling the leftover newline. Everything this series has taught, working as one program.</p><hr class="my-6" data-streamdown="horizontal-rule" style="border-top: 1px solid rgb(47, 47, 55); border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-right-color: rgb(47, 47, 55); border-bottom-color: rgb(47, 47, 55); border-left-color: rgb(47, 47, 55); margin-top: 0px; margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); height: 0px; margin-block: 1rem 0.75rem; --tw-space-y-reverse: 0; font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><h2 class="mt-6 mb-2 font-semibold text-2xl" data-streamdown="heading-2" dir="ltr" style="border-width: 0px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-color: rgb(47, 47, 55); border-image: initial; margin-top: 22.5px; margin-bottom: 7.5px; padding-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-size: 1.125rem; line-height: 1.35; --tw-space-y-reverse: 0; letter-spacing: -0.105469px; --tw-font-weight: 600; margin-block: 1.25rem 0.75rem; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; background-color: rgb(18, 18, 22);">Final Thoughts</h2><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);"><code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;is the least-used of C's three loops, but that's because it's specialized, not because it's weak. When the problem says&nbsp;<em style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5);">"this has to happen at least once"</em>&nbsp;— menus, prompts, retries, game rounds — no other loop expresses the intent as honestly.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">Lock in the essentials:&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">body first, condition after</span>;&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">the trailing semicolon is mandatory</span>;&nbsp;<span class="font-semibold" data-streamdown="strong" style="border: 0px solid rgb(47, 47, 55); outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); --tw-font-weight: 600; font-weight: 600;">declare condition variables outside the loop</span>; and reach for it when one guaranteed execution is what the problem demands.</p><p dir="ltr" style="border: 0px solid rgb(47, 47, 55); margin-bottom: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); margin-block: 0px 0.75rem; --tw-space-y-reverse: 0; color: rgb(252, 252, 253); font-family: &quot;Inter Variable&quot;, sans-serif; font-size: 13px; background-color: rgb(18, 18, 22);">With that, your loop toolkit is complete:&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">for</code>&nbsp;counts,&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">while</code>&nbsp;watches,&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">do-while</code>&nbsp;guarantees. One control-flow topic remains: the escape artists —&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">break</code>&nbsp;and&nbsp;<code class="rounded bg-muted px-1.5 py-0.5 font-mono text-sm" data-streamdown="inline-code" dir="ltr" style="border-width: 0px; border-color: rgb(47, 47, 55); margin-right: 0px; margin-left: 0px; outline-color: oklab(0.51773 0.045283 -0.122431 / 0.5); font-family: &quot;Geist Mono Variable&quot;, ui-monospace, SFMono-Regular, Menlo, Consolas, &quot;Liberation Mono&quot;, monospace; font-feature-settings: normal; font-variation-settings: normal; font-size: 0.8125rem; user-select: text; border-radius: 0.25rem; background-color: rgb(33, 34, 37); padding-inline: 5.625px; padding-block: 1.875px; line-height: 18.75px; padding: 0.1em 0.35em;">continue</code>&nbsp;— which you've already seen in action. Time to give them their own page.</p>

Code Example

#include <stdio.h>

int main() {
    int choice = 0;

    // Menu validation example
    do {
        printf("=== System Menu ===\
");
        printf("1. Start Server\
");
        printf("2. Stop Server\
");
        printf("3. Exit\
");
        printf("Selection: ");
        choice = 3; // Simulating valid user exit selection
        printf("%d\
", choice);
    } while (choice < 1 || choice > 3);

    printf("Selected Option: %d\
", choice);
    return 0;
}

Interview Question & Tips

Q: Why is `do { ... } while(0)` widely used in C `#define` preprocessor macros?
A: It allows multi-statement macros to be used safely inside `if-else` blocks without causing dangling `else` syntax errors when followed by a semicolon.
Q: What is the minimum number of times a `do-while` loop executes?
A: Exactly 1 time.

Best Practices

Use `do...while` for console menu loops and user input validation where user input must be collected before validating.
Academic Standards & Verified Citations

This study reference for C do...while Loop: Execute First, Check the Condition Later is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C Programming