Broad Network


Local Scope in C++

Scopes in C++ - Part 1

Forward: In this part of the series, I explain the meaning of local scope in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 1 of my series, Scopes in C++. In this part of the series, I explain the meaning of local scope in C++. I will first of all explain some features in scopes in general, before I go into local scope, proper. Even, before that, I will comment on this series and the other series to follow.

My C++ Volume
This series is part of my C++ tutorial volume. The C++ tutorial volume is made up of many series. It is divided into two halves. Completing and understanding the first half makes you a professional C++ coder. With that you can work (do a lot) with C++. After completing the first half you are strongly advice to complete the second half. The second half takes you to the level of an expert. At that level, you will be able to read the C++ specification (manual) and understand.

Actors of the Volume
This is the first part of the expert tutorial series set. From now onward, in the tutorials, the letter, I, means, me, the writer. You, means, you, the reader. We, means you, the reader, and me, the writer.

Let us go back to the topic of the day.

C++ Block
Scope in C++ is focus on the C++ block. The C++ block consists of statements in curly brackets. That is:

    {
        //statements
    }

The following constructs have blocks: for-construct, if-construct, while-construct and switch construct. Other constructs like the function construct have blocks and their scoping is interpreted in a similar way to what I will explain in this part of the series.

You can have nested blocks, as in:

    {
        //statements
        {
            //statements
        }
        //statements
    }

An identifier declared inside a block cannot be seen or used outside that block, everything being equal. It can be seen and used inside its block. With that, a block is called, a local scope. An identifier declared inside a nested (inner) block, cannot be seen in the outer block. On the other hand, an identifier declared in an outer block can be seen in the nested block.

A block of the if-construct, for-construct, while-construct and switch-construct is called a local scope. If a block has a nested block, both the outer and the nested blocks, form the local scope for the outer block. The nested (inner) block is a local scope, by itself.

I will spend the rest of this tutorial, illustrating local scope with the if, while, for and switch constructs.

The if-Construct
Consider the if-construct in the following program:

#include <iostream>
using namespace std;


int main()
    {

        int ident1 = 5;

        if (ident1 == (2+3))
            {
                int ident2 = ident1;
                cout << ident2;
            }
        

        return 0;
    }

In the if-block, ident2 declared inside the block, cannot be seen outside the block. That is, you cannot used it where “return 0;” is typed. There is ident1 in the if-condition. Any identifier in the if-condition, can be seen inside the if-block. Note: ident1 is actually declared in the outer (main) block; so it can be seen in the if-block.

The while-Construct
Consider the while-construct in the following program:

#include <iostream>
using namespace std;


int main()
    {

        int ident1 = 5;

        while (ident1 == (2+3))
            {
                int ident2 = ident1;
                cout << ident2;
                ident1 = 6;
            }
        

        return 0;
    }

The explanation of the previous code, applies to this code, where the if-condition is replaced by the while-condition.

The for-Construct
Consider the for-construct in the following program:

#include <iostream>
using namespace std;


int main()
    {

        for (int i=0; i<5; i++)
            {
                const char* str = "books";
                cout << i << " " << str << '\n';
            }
        

        return 0;
    }

The identifier, str, declared in the block of the for-construct, can be seen only in that block; it cannot be seen outside the block. The identifier, i, is declared in the parentheses of the for-construct. It is seen inside the block of the for-construct. The i identifier cannot be seen outside the parentheses and outside the for-block (construct).

The switch-Construct
Consider the switch construct in the following program:

#include <iostream>
using namespace std;

int main()
    {
        int hisVar = 10000;

        switch (hisVar)
            {
                case 10:
                    {
                        cout << "Value is small" << '\n';
                        cout << hisVar << '\n';
                        const char* str0 = "one";
                        cout << str0 << '\n';
                    }
                break;
                case 100:
                    {
                        cout << "Value is medium" << '\n';
                        cout << hisVar << '\n';
                        const char* str1 = "two";
                        cout << str1 << '\n';
                    }
                break;
                case 1000:
                    {
                        cout << "Value is large" << '\n';
                        cout << hisVar << '\n';
                        const char* str2 = "three";
                        cout << str2 << '\n';
                    }
                break;
                default:
                    {
                        cout << "hisVar is very large" << '\n';
                        cout << hisVar << '\n';
                        const char* str3 = "four";
                        cout << str3 << '\n';
                    }
            }

    return 0;
}

An identifier, such as, hisVar, declared in the outer block can be seen in any level of a nested block. An identifier, such as, str3, declared in a third level inner block, can be seen only at its particular inner block.

That is it for local scope. The other scopes are similar in explanation. We stop here and continue in the next part.

Chrys

Related Links

C++ Taking the Bull by the Horns
C++ Just After the Basics
Object Oriented Programming in C++
C++ Operators
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
Advanced Course
Storage Duration in C++
Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
More Related Links
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
C++ Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message