Embedded statement cannot be a declaration or labeled statement

Source: Internet
Author: User
Tags variable scope

Hi all friends, today, when I was writing a program, I suddenly encountered such a compilation error: Embedded statement cannot be a declaration or labeled statement.

A very common and low-level compilation error. However, it still involves figuring out why an error occurs.Variable scope:

The sample code is as follows:

Public static void main ()
{
If (true)
String text = "Charles ";
}

During compilation, the following error occurs: Embedded statement cannot be a declaration or labeled statement.

The correct statement is as follows:

Public static void main ()
{
If (true)
{
String text = "Charles ";
}
}

Or

 

Public static void main ()
{
String text;
If (true)
TEXT = "Charles ";
}

Summary:

That is to say, if a type variable is declared in the code block (IF:

1. Either declare that the variable is placed outside the code block (IF ),

2. Either use {}.

Otherwise, if you do not need to declare a variable in the code block {}, the variable cannot be accessed according to the scope of the variable. Therefore, the C # compiler will give an error ).

 

 

In addition, for ease of understanding, here I have excerpted the example of variable scope in the C # Getting Started classic to better understand the variable scope:

The following is an excerpt from C # Getting Started classic 3:

The scope of variables includes the code blocks that define them and the code blocks that are directly nested in them. This can also be applied to other code blocks, such as the code blocks of branch and loop structures. Consider the following code:

Int I;

For (I = 0; I <10; I ++)

{

String text = "line" + convert. tostring (I );

Console. writeline ("{0}", text );

}

Console. writeline ("last text output in loop: {0}", text );

The text variable of the string is a local variable of the for loop. This Code cannot be compiled because console. writeline () called outside the loop tries to use the text variable, which is beyond the scope of the loop. Modify the Code as follows:

Int I;

String text;

For (I = 0; I <10; I ++)

{

TEXT = "line" + convert. tostring (I );

Console. writeline ("{0}", text );

}

Console. writeline ("last text output in loop: {0}", text );

This Code also fails because the variables must be declared and initialized before use, while text is initialized in the for loop. The value assigned to text is lost when the loop block exits. However, you can make the following changes:

Int I;

String text = "";

For (I = 0; I <10; I ++)

{

TEXT = "line" + convert. tostring (I );

Console. writeline ("{0}", text );

}

Console. writeline ("last text output in loop: {0}", text );

This time, text is initialized outside the loop and its value can be accessed.

The value finally assigned to text in the loop can be accessed outside the loop.

It can be seen that the content of this topic takes a little time to grasp. In the previous example, an empty text string is assigned to the text string before the loop. In the code after the loop, the text will not be a Null String. The reason cannot be seen immediately.

This case involves the memory space allocated to the text variable. In fact, this is true for any variable.

Declaring only one simple variable type does not cause other changes. This value occupies a piece of memory space only after a value is assigned to the variable. If this activity occupies memory space in a loop, the value is actually defined as a local value, which is out of its scope outside the loop. Even if the variable itself is not localized to the loop, the values contained in the loop are also localized to the loop. However, an external value assignment in the loop ensures that the value is a local value of the Subject Code and is still in its scope within the loop. This means that the variable does not go beyond the scope before exiting the main code block, so it can be accessed outside the loop.

Fortunately, the C # compiler can detect the scope of variables. The response error message generated by the compiler helps us understand the scope of variables.

The last thing to note is that "best practices" should be adopted ". Generally, it is best to declare and initialize all variables before using them in the code block. An exception is to declare a loop variable as part of a loop block.

Charles Chen

Email: gotosunny@msn.com

MSN: gotosunny@msn.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.