Process Control Statements

Source: Internet
Author: User
Tags case statement

One, compound statement

The compound statement of the Java language is a statement in the entire chunk, so it is also called a fast statement. The opening parenthesis "{" begins, closing parenthesis "}" ends. Each statement in a compound statement is executed from top to bottom. The compound statement takes the whole block, and the lesson can be used anywhere a single statement can be used, and compound statements can be nested in compound statements. When using compound statements, be aware that a conforming statement creates a scope for a local variable that is part of a program, where a variable is created and can be used. If the variable is used outside the scope of a variable, only an error occurs.

Second, conditional statements

1. If condition statement

Used to tell the program to execute a certain statement in the case of a condition, and in another case to execute another statement. With the IF condition statement, you can choose whether to execute the statement that follows the condition closely. The keyword if is followed by a Boolean expression that is the condition, and if the expression returns true, then the statement that follows is executed: If False, the statement after the IF condition is not executed. The IF condition statement can be divided into simple if conditional statements, if...else statements, and If...else if multi-branch statements.

(1) Simple if condition statement

if (Boolean expression) {

Statement sequence

}

Boolean expression: The necessary argument, indicating that the result it returns must be a Boolean value, which can be a simple Boolean variable or constant, or an expression that uses a relationship or Boolean operator.

Statement sequence: selectable parameters. Can be one or more statements, and when the value of an expression is true, these statements are executed. If there is only one statement in the statement sequence, "{}" in the conditional statement can be omitted.

(2) If...else statement

If...else is selectively processed for a certain condition. It usually manifests itself as "if a certain condition is met, some sort of treatment is done, or another treatment is done".

if (expression) {

}

else{

Several statements

}

The value of the expression within the if () after () must be of type Boolean. If the value of the expression is true, the compound statement immediately following the IF statement is executed, and if the value of the expression is false, the compound statement behind the else is executed.

( 3 ) if...else If multi-branch statement

If...else if is used to handle multiple situations at a given time. It usually manifests as "if a certain condition is met, a certain treatment is done, otherwise if another condition is met, another processing is performed".

if (conditional expression 1) {

Statement sequence 1

}

else if (conditional expression 2) {

Statement Sequence 2

}

...

else if (expression N) {

Statement sequence N

}

Expression 1~n: Necessary parameters. Can consist of more than one expression, but the result returned is the Boolean type that was positioned.

Statement sequence: You can make one or more statements, and when the value of the conditional expression 1 is true, the statement sequence 1 is executed, and when the conditional expression 2 bit true, the sequence of statements is 2, and so on. When you omit any set of statement sequences, you can keep the "{}" outside of it, or you can replace "{}" with ";".

2. Switch multi-branch statement

switch (expression) {

Case Constant Value 1:

Statement Block 1

Break

...

case constant value N;

Statement block N

Break

Default

Statement block N+1;

Break

}

The value of an expression in a switch statement must be an integer, character, or string type, and the constant value 1~n must also be an integer, character, or string type. The switch statement evaluates the expression first, and if the value of the expression is the same as the value of the variable following a case, then several statements after the instance statement know that a break statement is encountered. At this point, if there is no break statement in the case statement, you will continue to execute several statements in the following cases until you encounter a break statement. If no value of a constant is the same as the value of the expression, the statement following the default is executed. The default statement is optional, and if it does not exist, and the value of the expression in the switch statement is not the same as the constant value of any case, switch does not do any processing.

Third, the circular statement

is to perform an operation repeatedly under certain conditions.

1. While loop statement

while () condition expression {

EXECUTE statement

}

When the conditional return value is true, the statement in "{}" is executed, and when the statement in "{}" is executed, the return value of the conditional expression is re-judged, knowing that the result returned by the expression is false and exits the loop.

2. Do...while Loop statement

Similar to the While Loop statement, the difference between them is that the while statement first determines whether the condition is in the execution loop body, and

The Do...while Loop statement executes a loop first and then determines whether the condition is set. This means that the program in the "{}" in the Do...while Loop statement must be executed at least once.

do{

EXECUTE statement

}

while (conditional expression);

One notable difference between the Do...while statement and the while statement is that the Do...while statement has a semicolon (;) at the end.

3. For Loop statement

(1) For statement

for (expression 1; expression 2; expression 3)

{

Statement sequence

}

Expression 1: Initializes an expression that is responsible for initializing the variable.

Expression 2: Loop conditional expression, position Boolean expression, specifies the loop condition.

Expression 3: The post-loop action expression, which is responsible for trimming the variable and changing the loop condition.

When executing a For loop, first executes the expression 1, completes the initialization of a variable, the next step to determine the value of the expression 2, if the value of expression 2 is true, then enter the loop body, after the completion of the loop immediately after the expression 3 is evaluated, which is usually an expression to increase or decrease the loop control variable. So the cycle is over. The second cycle begins with the expression 2, and if the expression 2 returns True, the loop continues, otherwise it jumps out of the entire for statement.

2. foreach statement

The foreach statement is a special simplified version of the for statement and cannot completely replace the for statement, but any foreach statement can be rewritten as a version of the for statement. foreach is not a keyword, and it is customary to refer to this special for-statement format as a foreach statement. The foreach statement provides a great convenience for programmers in terms of traversing arrays, etc.

for (element variable x: Traverse object obj) {

A Java statement that references X;

}

The element variable x in foreach does not have to be initialized.

Example: Create a reception in a project, define a one-dimensional array in the main method, and iterate through the array with the foreach syntax.

public class reception{

public static void Main (String[]args) {

int []array={10,7,9};

System.out.println ("Elements in a one-dimensional array are:");

for (int x;arry) {

The foreach statement, the variable referenced by int x, arr specifies the array to iterate through, and finally the X output

SYSTEM.OUT.PRINTLN (x);

}

}

}

The results of the operation are as follows:

The elements in a one-dimensional array are:

10

7

9

          

Process Control Statements

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.