LESSON 20 OF 52 · CHAPTER 02

Nested Repeat

Nested repeat loops, inner continue, and counters

ORIGINAL SOURCE

04-nested-repeat.srp

// Nested repeats produce a small grid; continue skips only the inner loop.
var accepted = 0;
var skipped = 0;
var total = 0;
repeat 4 as row {
    repeat 3 as column {
        if row == column {
            skipped += 1;
            continue;
        }
        accepted += 1;
        total += row + column;
    }
}
print(accepted, skipped, total);
EXPECTED OUTPUT

stdout

Verified
9 3 24

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Nested repeat loops, inner continue, and counters

Expected behavior

Prints 9 3 24

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.

नेस्टेड repeat और continue

बाहरी repeat 4 as row की हर पुनरावृत्ति में अंदर का repeat 3 as column चलता है। इस तरह 12 (row, column) जोड़ियाँ बनती हैं। row == column होने पर continue केवल अंदर वाले लूप के मौजूदा चक्कर को छोड़ता है। ऐसी तीन जोड़ियाँ हैं: (0, 0), (1, 1), (2, 2)। बाकी 9 जोड़ियों के row + column का योग 24 है।

अभ्यास: अगर repeat 3 as column की संख्या 4 हो जाए, तो कितनी जोड़ियाँ और कितने continue होंगे? अनुमान लगाकर कंपाइलर से जाँचें।

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

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/04-nested-repeat.srp scarp exec examples/02-control-flow/04-nested-repeat.srp

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

CHAPTER 02 · LAST LESSON

Ready for your next step? · आगे क्या सीखें?

This is the final example in this chapter. Try its verified coding lab before moving on. Your lesson progress changes only when you mark a lesson complete.

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