<java Basic > Compound Statements <6>

Source: Internet
Author: User

A compound statement is a statement that is based on the entire chunk, so it is called a block statement.
The compound statement is represented by the curly brace "{", "}".
In fact, we have contacted the compound statement, such as when defining a class or method, the class body is the "{}" symbol as the beginning and end of the tag, the method body is the same.
Each statement in a compound statement is executed from top to bottom, the compound statement is in the entire block, can be used anywhere a single statement can be used, and compound statements can also be nested compound statements.
The Compound statement instance (////* These two symbols represent comments):
Public class test{//Create Class
Public static void Main (string[] args) {//Main method
int a = 1; declares an int variable and assigns a value of 1
//b,c cannot be used in this scope.
                   {
//b,c can be used in this scope
int b = 2; declares an int variable and assigns a value of 1
int c = 3; declares an int variable and assigns a value of 1
Boolean D; Declaring a Boolean variable
                           {
d = b > C; The Boolean variable D is false at this time
                           }
                   }
         }
}

PS: When using compound statements, be aware that a compound statement creates a scope for a local variable that is part of a program that is created and can be used in that scope. If the variable is used outside the scope of a variable, an error occurs. Example: The B,C variable in this example.


<java basic > Compound statement <6>

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.