Swift programming language Learning 2.1--basic operator (in)

Source: Internet
Author: User

The operator is checking, changing. Merge value special symbols or phrases. For example, add + These two numbers are added (for example let i = 1 + 2).

such as more complex logic and operational implementation && (such as if Entereddoorcode && passedretinascan), or let I value plus 1 convenient operator increment operator ++i, etc.

Swift supports most of the standard C-language operators and improves many features to reduce general coding errors. For example, the assignment (=) does not return a value. To prevent errors caused by assigning an assignment to the place where you want to infer the equality operator (= =).

Numeric operator (+,-,*. /,%, etc.) will not agree to a value overflow, in order to avoid saving the variable because the variable is greater than or less than the range of its type can be caused by the exception result.

Of course you agree to use Swift's overflow operator to implement overflow. See overflow operator for details.

differs from C language. In Swift you are able to take the floating-point count (%), and Swift also provides an interval operator in which C language does not have a value between two numbers. B and A...B), which facilitates us to express values within a range.

This section describes only the basic operators in Swift, the advanced operators include advanced operators, how to define the operators themselves, and how to perform operator overloads of their own type.


Terms

Operators have unary, two, and ternary operators.

Unary operators operate on single-action objects, such as-a.

Unary operators are pre-and post-operators, and the predecessor operator is preceded by an operand (such as!b), and the post operator is immediately followed by the operand (such as i++).

The binary operators operate on two operands (such as 2 + 3), which are central because they are now between two operand objects.

The ternary operator operates three operands, and as in C, Swift has only one ternary operator, which is the ternary conditional operator (a?

B:C).

The value affected by the operator is called the operand. In expression 1 + 2, the Plus + is the two-dollar operator. Its two operands are the values 1 and 2.

Assignment operators

Assignment operation (A = b). Represents the value of B to initialize or update the values of a:

Let B = 10var A = 5a = b//A is now equal to 10


Assuming that the right side of the assignment is a multivariate group, its elements can be immediately decomposed into multiple variables or variables:

Let (x, y) = (1, 2)//now x equals 1, y equals 2


Unlike the C language and Objective-c, Swift's assignment does not return any value whatsoever.

So the following code is wrong:

Let (x, y) = (1, 2)//now x equals 1, y equals 2

This feature makes it impossible to write (= =) incorrectly because if x = y is the error code. Swift has helped you avoid these code errors from the bottom.

Numeric operations

Swift allows all numeric types to support the main arithmetic:

Addition (+)

Subtraction (-)

Multiplication (*)

Division (/)

1 + 2      //equals 35-3      //equals to * 3      //equals 610.0/2.5//equals 4.0


Unlike the C language and objective-c, Swift defaults to an overflow condition in numeric operations. But you can use Swift's overflow operator to reach your purposeful overflow (such as a &+ b).

See overflow operator for details.

The addition operator is also used for concatenation of strings:

"Hello," + "world"  //equals "Hello, World"


Two character values or a string and a character value. The addition generates a new string value:

L

ET dog:character = "D" Let Cow:character = "C" let Dogcow = dog + cow//Dogcow is now "DC"

See the characters for details. Concatenation of strings.

To find the remainder operation

The remainder operation (a% B) calculates how many times B is good enough to be able to accommodate a, returning the portion of the excess.

Attention:

The remainder operation (%) is also called modulo operation in other languages. But strictly speaking. We look at the result of the operator's operation on negative numbers, and the "redundancy" is more appropriate than "modulo".

Let's talk about how to get the remainder, calculate 9 4, you first calculate how many times 4 will just be able to fit into the 9:

Twice times, well, that remainder is 1

In Swift, this is the expression:

9% 4   //equals 1


In order to get the result of a% B,% calculates the following equation and outputs the remainder as a result:

A = (bx multiples) + remainder

When the multiplier takes the maximum value. Will just be able to fit into a.

Put 9 and 4 into the equation. We have 1:

9 = (4x2) + 1

The same method. I'm here to calculate-9% 4:

-9% 4//equals-1

Put 9 and 4 into the equation, 2 is the largest integer taken:

-9 = (4x-2) +-1

The remainder is-1.

When the negative number B is redundant. The symbol for B is ignored. This means that the result of a% B and a%-B is the same.

Calculation of floating-point number for remainder

Unlike the C language and objective-c,swift, floating-point numbers can be redundant.

8% 2.5//equals 0.5


In this example. 8 In addition to 2.5 equals 3 to 0.5, so the result is a double value of 0.5.


Self-increment and self-increment operations

Like the C language, Swift also provides an operator that facilitates the self-increment (+ +) and decrement (-) of the variable itself by adding 1 or minus 1.

Its operands can be shaped and floating.

var i = 0++i     //Now i = 1


A value of ++i,i is added 1 per call. In fact, ++i is shorthand for i = i + 1, and-i = shorthand for i-1.

+ + and--both pre-and post-op. Both ++i,i++,--i and i--are valid formulations.

We need to note that these operators have a return value after I change the i.

Suppose you just want to change the value of I. Then you can ignore the return value. But assuming you want to use the return value, you need to be aware that the return value of the front and back operations is different.

When + + is pre-set, the first self-build and return.

When the + + is behind. Return to increment first.

Like what:

var a = 0let B = ++a//A and B are now 1let c = a++//A is now 2, but C is the value of a self-increment 1


The above example. Let B = ++a first adds 1 and returns the value of a. So both A and B are the new value 1.

and let C = a++. The value of a is returned first. Then a adds 1. So C gets the old value of a 1, and a plus 1 turns 2.

Unless you need to use the i++ feature. It is recommended that you use ++i and-I, because it is more consistent with our logic to return this behavior first.

unary minus

The sign of a value can be toggled using a prefix-(that is, a unary minus):

Let three = 3let Minusthree =-three       //Minusthree equals -3let plusthree =-minusthree   //Plusthree equals 3, or "negative minus 3"


A unary minus (-) is written before the operand with no spaces in the middle.

Unary Plus

Unary plus (+) does not change the value of the operand regardless of what is changed.

Let Minussix = -6let alsominussix = +minussix  //Alsominussix equals-6


Even though one dollar + does not work hard. But when you use a unary minus to express negative numbers, you can use a unary plus to express positive numbers, so your code will have symmetrical beauty.

Compound assignment (Compound assignment Operators)

Like the powerful C language, Swift also provides a compound assignment operator that combines other operators and assignment operations (=), with the addition of an assignment (+ =) as a sample:

var a = 1a + = 2//A is now 3


The expression A + = 2 is a shorthand for a = a + 2. An add-and-assign operation completes the addition and assignment of two things.

Attention:

Compound assignment operation has no return value, let B = a + = 2 Such code is an error.

This differs from the self-increment and decrement operators mentioned above.

There is a complete list of composite operators in the expression section.

Comparison operation

The comparison operations in all standard C languages can be used in Swift.

equals (A = = b)

Not equal to (a! = b)

Greater than (a > B)

Less than (a < b)

Greater than or equal to (a >= b)

Less than or equal to (a <= b)

Attention:

Swift also provides the identity = = and non-identical!== to infer whether two objects refer to the same object instance.

Many other details are in class and structure.

Each comparison operation returns a Boolean value that identifies whether the expression is true:

1 = = 1  //True, because 1 is equal to 1  //True, because 2 is not equal to > 1   //True, because 2 is greater than one < 2   //True, because 1 is less than 21 >= 1  //True, because 1 is greater than or equal to <= 1  //False, since 2 is not less than or equal to 1


The comparison operation is used more for conditional statements. If condition:

Let name = "World" if name = = "World" {   println ("Hello, World")} else {   println ("I ' m sorry \ (name), but I don ' t rec Ognize you ")}//exit" Hello,world "due to ' name ' It equals" world "


Left and right if declarations. See control flow.

Swift programming language learning 2.1--base operator (in)

Related 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.