Process Control (Object oriented article)

Source: Internet
Author: User

Process Control statements mainly include sequential statements, SELECT statements, loop statements, and jump statements.

Sequential structure: The sequential structure is the most common process structure in the program, and the statements are executed from top to bottom in order of occurrence. The sequential statement does not jump and select during execution until the program finishes. Examples are as follows

Run results

The statements in the main () method are executed sequentially from top to bottom, defining the variable first, assigning the variable next, and then typing the value result.

Select structure: The selection structure consists of two statements, if statements, and switch statements. First, we explain the conditions of the program jump-operator, operator including relational and logical operators, and conditional operators.

Relational operators: There are 6 common relational operators, including ==,! =,>,<,>=,<=, the result of the relational operation is a Boolean, and the result is only ture or false.

There are 3 types of logical operators, including | |,&&,! (The operation priority is from low to high).

Conditional operator: A ternary operator with the form of an expression 1? Expression 2: Expression 3. (My memory of the operator is 1 true for 2, 1 false for 3). Example below b=a<0? -A:A, where the value of a is greater than 0, then assign its value to B, and if a is negative, assign a negative value to B, which implements the function of finding the absolute value of a number.

If statement: The most common choice statement, which determines whether to execute the following inline statement, based on the value of the Boolean expression, for example:

Run results

If-else Statement: Example of a program that determines the parity of a numeric value:

Run results

If-else If-else: Multiple If statements, is a multi-branch selection structure. Examples are as follows:

Run results

Switch statement: the ability to implement multiple choices. Format is switch (control expression) {case (constant expression: statement 1 break;) default: statement;}

Note: The 1.switch statement calculates the value of the expression in parentheses first, 2. Depending on the value of the expression to determine whether the selected value after the case is met, if all the case values are not met, execute the statement contained by default and leave the switch statement after execution is complete. 3. If the selected value of a case conforms to the result of the expression, the statement contained in the case is executed until the break statement is encountered before leaving 4. If there is no statement defining the execution of the default, then nothing is executed, leaving the switch statement directly. Examples are as follows:

Run results

Loop structure: Includes a while loop and a for loop, and can also use loop nesting to complete complex program control operations.

The while statement is the simplest looping statement that conditionally executes the inline statement 0 times or several times. While the execution of a while statement evaluates the value of the expression, and if the value of the expression is true, executes the statement in the loop body and resumes the loop otherwise exits the loop, executing the statement after the while. Example a simple example that uses a while loop to calculate an integer within 10 and:

Run results

Do-while loop: Called Until loop, it first completes the loop body and then determines the loop condition. This is the difference from the while loop statement, in the format do{statement} while (expression)

For loop: Introduction of the For loop before introducing the "+ +" and "minus"--"operator, the function of the self-addition operator is to make the variable plus 1, the decrement operator makes the variable minus 1. The example is as follows: int A;int b=1;a=b++;(B is assigned to a, then B is self-increment, and the result is a==1,b= =2), A=++b; (b first Self-increment, then the value of B is assigned to a, the result is a==2,b==2), and the self-subtraction and self-addition are the same principle.

Jump statement: The Java Jump statement consists of two types, namely break statement and continue statement.

Break statement: You can cause the program to immediately exit the loop structure, and instead execute the statement below the loop body structure, which is also called an interrupt statement, often used to jump out of a loop at the appropriate time. Examples of features are:

Run results

Continue statement: This loop is ended and the loop condition is tested immediately to determine whether the next loop is to be performed. Examples are as follows:

Run results

Summary: This chapter mainly introduces the various process control statements in Java, including the choice structure If/seitch statement, the circular structure while/do-while statement and the Jump statement break/continue statement, all is the Java Learning Foundation, needs the key to grasp.





Process Control (Object oriented article)

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.