What is the difference between scope and block?

后端 未结 9 1152
旧巷少年郎
旧巷少年郎 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 ??

提交回复
热议问题