What is the difference between scope and block?

后端 未结 9 1072
旧巷少年郎
旧巷少年郎 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 06:06

    For programming languages in general, the scope of a block (also known as block scope) is just one kind of scope; see https://en.wikipedia.org/wiki/Scope_(computer_science)#Levels_of_scope.

    Some languages, like Python, do not have blocks nor block scopes (but instead have function scopes, global scopes, etc.).

提交回复
热议问题