Javase Getting started learning the operators and statements of 6:java basic syntax (top)

Source: Internet
Author: User

Go on: Javase Introductory Learning 5:java Basic Grammar (a) look at the basic syntax of Java.

       Five Operators

An operator is a "function" symbol that informs Java about the operation. Example. We need to set the value of the variable age to 20. This is

A "=" is required to tell the program that an assignment is required.

the commonly used operators in the Java language can be categorized into the following types: arithmetic operators, assignment operators, comparison operators, logical operators, and conditional transport characters.  

(1 ) arithmetic operators

Arithmetic operators are primarily used for major arithmetic operations. such as addition, subtraction, multiplication, division, and so on.

Arithmetic operators that are frequently used in Java:


Among them, + + and--both can be left on today's operands, and now on the right, but the results are different.


% is used to find the remainder, also known as the modulo operator.

(2) assignment operator

An assignment operator is a symbol that specifies a numeric value for a variable or constant.

If you can use "=" to assign the result of the right expression to the left operand.

Java supports the use of assignment operators frequently. For example, see the following table:


(3 ) comparison operator

A comparison operator is used to infer the size of two data. For example: greater than, equal to, not equal to. The comparative result is a Boolean value (True or false).

The comparison operators commonly used in Java are seen in the following table:


1) >, <, >=, <= only support the left and right side operands are numeric types.

2) = =, the operands on both sides can be numeric types, and can be reference types.

(4 ) logical operators

Logical operators are primarily used for logical operations.

The logical operators commonly used in Java are as seen in the following table:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

We can understand logical operators from the perspective of "voting":

1) and: All people are required to vote for permission, the ability to pass an issue.

2) or: Just ask a person to vote for permission to pass an issue.

3) Non: Someone originally voted to allow. It is possible to invalidate a vote by using a non-operator.

4) XOR: There is only one person who can vote to pass an issue.

When a logical operator is used. We will encounter a very interesting "short circuit" phenomenon.

For Example: (one>two) && (one<three). Assuming that the left One>two execution result is false, the system feels there is no need to execute

Right side of the one<three.

Similarly, in (one>two) | | (One<three), assuming you can determine the execution result of the left expression to true, the system will feel that there is no necessary

To run the One<three on the right side again.

(5) conditional operator

The conditional operator (?:) is also known as the ternary operator.

Syntax form: boolean expression? Expression 1: Expression 2;

Operation procedure: Assume that the value of the Boolean expression is true. The value of expression 1 is returned, otherwise the value of expression 2 is returned.

(6) operator Precedence

The so-called priority is the order of operations in an expression.

The precedence of operators commonly used in Java is as seen in the following table:


       Six Statements (1) If of a Java conditional statement

In life, we often need to make inferences first. Then decide whether or not to do something.

For example, if the test result is greater than 90, reward a

IPHONE 5S for such a "need to infer the condition before the condition is met, run the situation ", you can make implemented with an IF condition statement.

Grammar:


Run the process:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "style=" font-size:18px "> such as:


assuming that the if condition is set, the run statement has only one line. is the ability to omit curly braces. However, if you run a statement with multiple lines, the curly braces are not or

lack of.      

(2) If...else of Java conditional statements

the operation of the If...else statement is one step more than the IF statement: when the condition is true, the code block of the if part is run; when the condition is not true, into the Else section.

For example, if the test score is greater than 90, then an iphone 5S is rewarded. Otherwise the penalty does 500 push-ups .


Grammar:

Run the process:

such as:


(3 ) Multiple if of a Java conditional statement

multiple IF statements. When condition 1 is not satisfied, the inference of condition 2 is carried out, when the preceding conditions are not established. Before the generation in the Else block is run

Code. Like what. If the test result is greater than 90, then reward an iphone 5S, assuming the score is between 70 and 90, then reward a red meter, no

Then 500 push-ups will be punished.

Grammar:


Run the process:

such as:


(4 ) nested If of Java conditional statements

a nested If statement only infers the condition of the inner if if the condition of the outer if is true. Like what. Schedule of activities. Suppose today is working day.

the go to work, assuming today is the weekend, then go out to play, at the same time, if the weekend weather is clear, then outdoor play play. Or go to the indoor playground.

Play.

Grammar:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Run the process:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Such as:


(4) Switch of Java conditional statements

Using the switch statement is more concise when you need to infer an equivalent of an option. For example: according to the position of the exam, give the top 4 different prizes.

First prize notebook one, second prize IPAD2 one, third place. Reward Mobile Power one; the last one to reward u disk.

Grammar:


Run procedure: When the value of the expression after switch is the same as the value after the case statement, it starts from that position until it encounters a break statement or

The switch statement block ends, assuming that no matching case statement runs the code of the default block.

Such as:


Precautions:

1) Switch the value of the expression after the parentheses must be an integer or character type.

2) The value following the case can be a constant value, such as 1, 2. can also be a constant expression. But not a variable or an expression with a variable.

Type, such as a*2.

3) After case matching, run the program code in the matching block, assuming that no break will continue to run the contents of the next case block until it encounters

The break statement or the SWITC statement block ends. It is not recommended to use such as:


Execution Result:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

4) The ability to combine the same case statements, such as:


5) The default block can be anywhere in today's position. can also be omitted.



Javase Getting started learning the operators and statements of 6:java basic syntax (top)

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.