#从零开始学Swift2.0# No.2 operators and expressions

Source: Internet
Author: User
Tags arithmetic operators

operator

Operator is used to execute program code operations, which are performed on one or more operand items. For example: 2 + 3, whose operands are 2 and 3, while the operator is "+". So "+,-, *,/" are all operators.

What kinds of operators are there?

The main operators in the swift language include arithmetic, relationships, logical operators, and so on.

Arithmetic operators: +,-, *,/,%, + +,--etc.

+: Adds a sum operation, and can also be used for string-type connection operations.

-: Reduce the calculation of the difference.

*: Multiply the quadrature operation.

/: Except rounding operations.

%: remainder operation.

+ +: Self-added i++ means first to take the value plus one, ++i means to add and repeat the value.

---: self-subtraction I--means that the first value and then minus one,--I means to reduce the number of repeated values.

Relational operators: <, = =, >=, <=,! =, etc.

Logical operator:! (non), && (with), | | (or) and so on.

In addition to some of the commonly used operators mentioned above, there are some other operators:

. : an operator of a reference, an instance call property, a method, and so on.

? : Used to declare an optional type.

! : A forced unpacking of an optional type value.

-A: Describes the method return value type.

:: Used for dictionary collection to split key-value pairs.

In these operators, some operators that require only one operand are called the monocular operator (or unary operator). Such as:

Logical non-operator "! ", the self-decrement operator" + + ","--", the minus sign operator"-", The pointer operator" * "and the Fetch address operator" & ", and so on.

An operator that requires two operands is called a binocular operator, such as:

Arithmetic operators: "+", "-", "*", "/", "%", relational operator: "<", ">", "= =", ">=", "<=", "! =", logical operator: "& ;& "," | | | " such as.

An operator that requires three operands is called a trinocular operator, and the trinocular operator is only "?:".

An expression

In Swift, there are 3 forms of expression: No data type specified, data type specified, use "; ".

Do not specify a data type:

Specify the data type:

Use "; " :

Be careful you will find that in swift language, a statement can end without adding "; "Can also be added"; ", but there is a situation which must be used"; , that is, when multiple statements are written in one line, you need to differentiate the statements by semicolons.

#从零开始学Swift2.0# No.2 operators and expressions

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.