LESSON 06 OF 52 · CHAPTER 01

Scopes

Nested blocks and shadowed names

ORIGINAL SOURCE

06-scopes.srp

var total = 1;
{
    bind total = total + 10;
    print(total);
    {
        var total = total * 2;
        total += 1;
        print(total);
    }
    print(total);
}
total += 2;
print(total);
EXPECTED OUTPUT

stdout

Verified
11
23
11
3

UNDERSTAND THE CODE

Explanation & practice

What this example teaches

Nested blocks and shadowed names

Expected behavior

11, 23, 11, 3

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.

स्कोप और शैडोइंग

घुँघराले कोष्ठकों के भीतर नया स्कोप बनता है। उसी नाम की स्थानीय बाइंडिंग बाहरी बाइंडिंग को बदले बिना छिपा सकती है।

अभ्यास: चारों आउटपुट में कौन-सा total प्रयुक्त है, समझाइए।

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/01-basics/06-scopes.srp scarp exec examples/01-basics/06-scopes.srp

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

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