Home  >  Article  >  Java  >  What are statements in java

What are statements in java

王林
王林Original
2019-11-19 11:13:494166browse

What are statements in java

In Java, a statement is the smallest unit, and each statement must use a semicolon as the terminator.

In addition, Java does not have any other restrictions on statements. Developers are free to write statements in a way that suits their own style.

For example, you can put a statement in multiple lines. The example is as follows:

String str = "Apple "
+"Banner "+"Pear "
+" Orange";

Since Java uses a semicolon as the terminator of a statement, the above 3 lines of code will be considered by Java. one statement since there is only one semicolon in these 3 lines. However, we do not recommend writing statements this way.

Also, writing multiple statements on one line is allowed because of the use of semicolons as delimiters. For example, the following sample code is also correct.

int a = 0,b,c;b=a+10;b++;c=a*b;System.out.println(c);

The above puts 5 statements on one line.

In order to make the program statements more beautiful, easier to read and eliminate errors, the following rules are generally used to format the source code:

1. Write only one in a line statement, and use spaces and blank lines to ensure that the statement is easy to read.

2. Use the Tab key to indent to the right within each compound statement.

3. Braces are always placed on a separate line to facilitate checking whether they match.

Empty statement

The so-called empty statement does nothing in the program and does not contain any actual statements. In programs, empty statements are mainly used as empty loop bodies.

The syntax format of an empty statement is as follows:

; // 其实就是一个分号

Executing an empty statement transfers control to the end point of the statement. This way, if the empty statement is reachable, the end point of the empty statement is also reachable.

Expression statement

In many high-level languages, there are special assignment statements. In Java, assignment is an operator, so there are only assignment expressions. Adding a semicolon after the assignment expression makes it a standalone statement.

The following are sample statements for some expressions:

3.1415926;
(a+b)/2;
x*y*z-y+(20-x);

These expressions can be recognized by the Java compiler, but since they do not perform any operations on the program, they have no meaning.

General expression statements should be able to complete an operation, such as modifying the value of a variable or serving as a function parameter. This is done by specifying a variable on the left side of the expression to store the value of the expression, or by passing the expression to a function.

The following is the modified expression statement:

pi=3.1415926;
output(pi); // 将pi的值传递到output()函数中作为参数
sum=(a+b)/2;
printf("%f",sum); // 将sum的值传递到printf()函数输出
temp=x*y*z-y+(20-x); // 将表达式的值保存到temp变量中

Compound statement

A compound statement is also called a statement block, which is a A combination of statements so that multiple statements can be treated as a single statement.

The syntax format of a compound statement is as follows:

{
    statement-list // 语句列表
}

You can see that it consists of an optional statement-list expanded within curly brackets. statement-list is a list consisting of one or more statements. If statement-list does not exist, the statement block is said to be empty.

The execution rules are as follows:

1. If the statement block is empty, control goes to the end point of the statement block.

2. If the statement block is not empty, control goes to the statement list. When control reaches the end of the statement list, control goes to the end of the statement.

Example 1:

Create a statement block containing 3 statements.

{
    width = 10; // 为width变量赋值
    height = 90; // 为height变量赋值
    area = width * height; // 计算width变量和height变量的乘积
}

After the above code is executed, the value of the sum variable is 900. The above statement block contains 3 statements within curly brackets. The first statement assigns a value to the width variable, the second statement assigns a value to the height variable, and the third statement multiplies width and height and stores the result in the sum variable.

Recommended tutorial: java introductory tutorial

The above is the detailed content of What are statements in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn