Detailed explanation and examples of Java statement block

Java statement block

I still remember when I first read the programming books of C, C + + and Java, there was an introduction to statement blocks, but I didn't understand what statement blocks were at that time. It is also said in the code encyclopedia that sentences with similar functions should be organized together to form idiom sentence blocks, and then separated from other sentence blocks with blank lines. But this is only a statement block in human understanding, not a statement block in the sense of real programming language.

From my understanding, a program definition should be a collection of related statements with the same variable scope. It seems that it should be enclosed with {}, such as the logic in the control structure. I think the key point is variable scope, that is, if the same local variable can be used, it is a statement block in the sense of program. Let's take an example:

For the second case statement, the variables defined in the previous case can still be used, so the whole switch () {} is a statement block.

However, if the statement block flag is added to each case statement, it is different:

Adding {} separates the two case statements to form two statement blocks. They have their own variable scopes and do not affect each other. It doesn't hurt to use the same name or define it again.

The purpose of explaining these is to use {} as much as possible to form real statement blocks. The biggest advantage is that it can form variable scope and avoid too large variable scope, which improves readability and reduces the possibility of error.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>