What is the difference between scope and block?

后端 未结 9 1070
旧巷少年郎
旧巷少年郎 2021-01-04 05:16

I saw a piece of code in a book, which is as follows:

x = 10;
if(x ==10) { // start new scope
int y = 20; // known only to this block
x = y * 2;
}

相关标签:
9条回答
  • 2021-01-04 05:45

    As per definition of Block

    A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

    So

    {   //block started
    
    }    //block ended
    

    What ever the variables declared inside the block ,the scope restricted to that block.

    Is that making sense ??

    0 讨论(0)
  • The oracle docs define block as

    A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed

    @Mhd.Tahawi has already told what a scope is.

    One thing I should point out,

    switch(something){
        case somethin1:
            line1
            line2
            break;
        case someting2:
            line3
            line4
            break;
    }
    

    line{1,2,3,4} are within the same scope thus block because I did not begin and end each case with braces. Unlike python, indentation does not imply a block

    0 讨论(0)
  • 2021-01-04 05:49

    Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access a variable.

    It is very useful to be able to limit a variable's scope to a single function/block. The variable will have a limited scope. This way, changes inside the function/block can't affect the main program in unexpected ways.

    0 讨论(0)
  • 2021-01-04 05:50

    From Mozilla's A re-introduction to JavaScript (JS Tutorial)

    An important difference between JavaScript and other languages like Java is that in JavaScript, blocks do not have scope; only functions have a scope. So if a variable is defined using var in a compound statement (for example inside an if control structure), it will be visible to the entire function. However, starting with ECMAScript 2015, let and const declarations allow you to create block-scoped variables.

    Given the examples for var and how they differ from those for int and const, we can gather that, in JavaScript, a variable declared with var is visible outside of its immediate "scope," as we would typically use the word in other languages.

    In the example given, something declared with var in a for-loop is available from anywhere in the function, both before and after the for-loop in which it was declared.

        // myVarVariable *is* visible out here
    
        for (var myVarVariable = 0; myVarVariable < 5; myVarVariable++) { 
          // myVarVariable is visible to the whole function 
        } 
    
        // myVarVariable *is* visible out here 
    

    This is in contrast to anything declared with let and const, which are limited to the block in which they are declared, in much the way variables are limited to the immediate scope of a single set of curly braces { } in other languages.

    Moral of the story: be careful with var if you are used to other languages. It is much more powerful and visible than you would expect.

    0 讨论(0)
  • 2021-01-04 05:59

    a scope is where you can refer to a variable. a block defines a block scope a variable defined inside a block will be defined only inside that block and you can't reference it after the end of block.

    so in this code if you try something like:

    x = 10;
    if(x ==10) { // start new scope
    int y = 20; // known only to this block
    x = y * 2;
    }
    
    y = 5; // error y is out of scope, not it is not defined
    

    because what you have here is a local scope
    other kinds of scope in java are class scope (for example), a member of a class has a class scope so it is accessible anywhere inside a class.

    the basic rules for scope are:

    1. The scope of a parameter declaration is the body of the method in which the declaration appears.
    2. The scope of a local-variable declaration is from the point at which the declaration appears to the end of that block.
    3. The scope of a local-variable declaration that appears in the initialization section of a for statement’s header is the body of the for statement and the other expressions in the header.
    4. A method or field’s scope is the entire body of the class. This enables non-static methods of a class to use the fields and other methods of the class.
    0 讨论(0)
  • 2021-01-04 06:01

    From the Java language specification:

    14.2. Blocks:

    A block is a sequence of statements, local class declarations, and local variable declaration statements within braces.

    6.3. Scope of a Declaration

    The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible (§6.4.1).

    In a block, you can declare variables. A scope defines the region, where you can access a declared variable by its simple name.

    0 讨论(0)
提交回复
热议问题