LESSON 18 OF 52 · CHAPTER 02

While Loops

Counting, conditional skipping and early exit

ORIGINAL SOURCE

02-while-loops.srp

// Iteration with nearest-loop control and checked accumulator arithmetic.
var index = 0;
var total = 0;
while index < 10 {
    index += 1;
    if index == 3 { continue; }
    if index > 5 { break; }
    total += index;
}
print(total);
EXPECTED OUTPUT

stdout

Verified
12

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Counting, conditional skipping and early exit

Expected behavior

Prints the accumulated sum

Practice: Read the source before looking at the output. Predict the values and their order, then use the pinned compiler to execute this lesson and compare the exact fixture. Identify which operation produces the result.

while, break, continue

while लूप शर्त सही रहने तक चलता है। continue वर्तमान चक्कर का बाकी भाग छोड़ता है; break लूप से बाहर निकलता है।

अभ्यास: कौन-से index total में जोड़े गए हैं?

Explanations are sourced from the project's English chapter guide and हिन्दी अध्याय. The Scarp program and output are unchanged.

SOURCE-BACKED SELF-CHECK · स्रोत-आधारित अभ्यास

Quick checkpoint / त्वरित अभ्यास

What total does the while loop print after skipping 3 and breaking above 5?3 को छोड़ने और 5 से ऊपर जाने पर लूप रोकने के बाद while लूप कौन-सा total प्रिंट करता है?

Reveal the source-backed answer / सही उत्तर देखें

12 — The loop adds 1, 2, 4 and 5. It skips 3 with continue and exits at 6, so the sum is 12.12 — लूप 1, 2, 4 और 5 को जोड़ता है। continue से 3 छूट जाता है और 6 पर break होता है, इसलिए योग 12 है।

This question checks understanding of a recorded compiler output. It does not run Scarp in the browser. यह प्रश्न सत्यापित आउटपुट पर आधारित है; ब्राउज़र में Scarp नहीं चलता।

TRY IT LOCALLY

Verify, then execute

Use the pinned compiler from the course root. Keep any imported _support files next to the source.

scarp verify examples/02-control-flow/02-while-loops.srp scarp exec examples/02-control-flow/02-while-loops.srp

The website does not run code. Read the CLI guide ↗

Pinned compiler: Scarp v0.0.6-alpha.3 · ceff07d9014f. No simulated output.