Fifth section algorithm

Source: Internet
Author: User

Operator (method composition Two: The composition of the algorithm in the method body one)

1. Operation Operation

1.1 Overview of Operations

Types of 1.1.1 Operations: In addition to the usual plus (+), minus (-), multiply (*), except (\), the modulo operation (%) and the self-increment (+ +) and the decrement (--) operations are included

1.2 Use of the operation

1.2.1 Usage Overview:

Use the "+ +" and "--" operators: You can increase the value of a variable by 1 or minus 1

1.2.2 Use Cases

(1) If it is written before the variable, it means adding 1 or minus 1 before using the variable.

(2) If the variable is written, it means that the variable is used and then added 1 or minus 1.

2. Relational operations

2.1 Overview of Operations

2.1.1 Effect

Used to determine the size relationship between data

2.1.2 Type

Includes more than (>), less than (<), greater than or equal (>=), less than or equal (<=), equals (= =), not equal to (! =) Six operators

2.2 Use

Use a Boolean type to indicate whether this is the state, or false if the relationship is set to True

3. Logical operation

3.1 Overview

3.1.1 Effect

Used for logical operations, it is based on relational operations, and when two relational operations require consideration, a logical operator can be used

3.1.2 Classification

With (&&), or (| | ) and non-(!)

Single & and Single!

3.2 Use

3.2.1 Usage Overview

A variable or expression that participates in a logical operation is a Boolean type, and the result of the operation is also a Boolean type

3.2.2 Usage Type

(1) &&

The,&& expression is true when the variable b1 and the variable B2 are both true.

Short-circuit logic: for "&&", when the first relational expression can determine the result of the entire expression, it will not be judged after the second expression

(2) | |

When the variable b1 and the variable B2 have a true, | | The expression is true.

Short-circuit logic for "| |", when the first operand is true, the second operand is not judged, because at this point the final result must be true regardless of the second operand.

(3)! There is only one expression, and!b1 is true when B1 is true and!b1 is false,b1 to false.

(4) &

Without a short-circuit effect, the front conditions are met and the subsequent

(5) |

Without a short-circuit effect, the front conditions are met and the subsequent

4. Assignment operation

4.1 Overview

4.1.1 Effect

Used to assign a value to a variable

4.1.2 Classification

(normal) =

(extension) subtraction =; + =;

Note: There is a forced type conversion of the hidden (as if it were---)

4.2 Use

4.2.1 Assigning values to variables

4.2.2 The value of the value that is assigned.

In addition to assigning the right expression to the left, it has the following characteristics: The assignment expression itself also has a value, the value of which is the value assigned

SYSTEM.OUT.PRINTLN (index = num% 5); The result is: 3, the assignment expression itself has a value

5, character connection operation

Use "+" for string connections

When the + sign acts on two numeric type variables, it is an arithmetic operation.

When a variable on either side of the + sign has a string type, which is "", it is a string connection, and the concatenated result is a string type

6. Condition (trinocular) operation

6.1 Overview

The conditional operator is also called the "Trinocular" operator, and its structure is: a Boolean expression? Expression 1: Expression 2

6.2 Use

The rules for the 6.2.1 conditional operator are as follows:

(1) The Boolean expression is calculated first;

(2) If the value of the Boolean expression is true, the value of the entire expression is the value of expression 1;

(3) If the value of the Boolean expression is False, the value of the entire expression is the value of expression 2.

int flag = a > B? 1:-1;

6.2.2 Nested use

The so-called nesting refers to the condition (trinocular) Expression: "Boolean expression?" Expression 1: Expression 2 in the expression 1 or expression 2 is also a conditional (trinocular) expression, which is equivalent to multiple judgments;

Structure statement (method composition two: The composition of the algorithm in the method body two)

1. Overview

2. Classification

Order, branching, looping

3. Branch structure If-else

3.1 Overview

The branching structure is judged on a condition, and if the condition is met then a is executed, or B is executed.

3.2 Use

3.2.1 Usage Type:

(1) Run some statements when the condition is satisfied, and do not run the--IF structure when the condition is not satisfied.

(2) Run certain statements when conditions are met; Run additional statements--if-else structure when conditions are not met

3.2.2 Using syntax

Statement 0;

if (logical expression) {

Statement 1;

Statement 2;

}

Statement 3;

As the above statement, the following steps are performed:

Step one: Execute statement 0;

Step two: Determine the value of the logical expression, the value of this expression is a Boolean type, that is, true or false. This can be either a relational expression or a logical expression.

    1. If the value is true, the statement in the IF statement block is executed;
    2. If the value is false, the IF statement block is skipped;

Step three: Execute the statement 3 statement.

3.2.3{}

{} There is only one statement, you can omit {}, but not recommended;

4. Branch Structure Switch-case

4.1 Overview

Switch-case is a special branch structure.

4.2 Use

4.2.1 No break.

switch is the integer constant value after the case as the entry, and if the value is equal, start executing the statement following it. If no matching value is found, only the default statement is executed;

Note 1: There are two issues to be aware of when using switch

First, the constant value after the case must be different

Second, the value of the integer expression after switch must be an integer or character type.

Thirdly, starting with JDK 7.0, switch-case can support string expressions, which will make it easier for the program to operate.

4.2.2 has a break.

As the break statement in the above program is the function of jumping out of the switch structure;

When the execution process encounters a break, the execution jumps out of the switch

5. Circulation

5.1 Overview

5.1.1 Concept

Loop is a kind of computer processing process that executes some code repeatedly in programming language, and it is a set of identical or similar statements that are executed in a regular and repetitive way.

5.1.22 Elements

Two elements, one element is the loop body, that is, repeated execution of the same or similar statements, the second factor is the condition of the cycle, that is, the condition of the loop to continue to carry on, often in the way of the number of cycles reflected.

5.1.3 classification

While, Do-while, for.

5.1.4 syntax

(1) Break: Allows the program to terminate the loop and execute the statements following the loop, often with conditional statements

(2) Continue: function to skip the remaining statements in the loop body and perform the next loop

5.1.5 Analysis Problem 3 part

Loop variable Initial state

Cycle conditions

Changes in cyclic variables

5.2while Cycle

When does 5.2.1 use

Judge and execute first.

5.2.2 Syntax

while (Boolean expression) {
statement block;}

The while statement is executed by first calculating the value of the Boolean expression and then judging, if the value is true, the statement block is executed, the statement block is executed, the value of the Boolean expression is evaluated again, and if true, the statement block continues to execute, Statement that executes while after exiting while loop until the value of the Boolean expression is False

5.3DO---While

When does 5.3.1 use

Execute once and then judge

5.3.2 syntax

do {

Statement block

} while (Boolean expression);

The Do-while statement executes the statement block first, then the Boolean expression, and if true, executes the statement block again, repeating this until the value of the Boolean expression is false. That is, the Do-while statement executes the statement block first, regardless of whether the Boolean expression is true.

5.4for Cycle

5.4.1 When to use

The case where a statement or statement block is repeated for a predetermined number of times

5.4.2 syntax

for (expression 1; expression 2; expression 3) {

Statement block (loop body)

}

Special use of 5.4.3 expressions

Special Mode 1: expression 1 position content is empty

Special Mode 2: Expression 3 When the content of the position is empty

Special Mode 3: Expression---Dead loop when the content of the all-in-zero position is empty

Special Mode 4: Diversification of Expressions 1 and 3-bit content

Expression 1 and expression 3 in the three expression in the For statement can use a comma expression, which is an expression consisting of multiple expressions separated by the "," operator, calculated from left to right

for (int i =1, j = 6;  I <= 6; I +=2, J-=2)

Fifth section algorithm

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.