Swift Language Guide (ix) basic operators of Swift language

Source: Internet
Author: User
Tags arithmetic arithmetic operators range


An operator (operator) is a special symbol or phrase used to examine, change, or combine values. For example, the plus operator has two numbers added (such as let i = 1 + 2), and some more complex operators, such as logic and Operators (&&) (e.g. if Entereddoorcode && Passedretinascan) And the self-increasing operator (++i) (a convenient notation for the value of I plus 1).



Swift supports most of the operators of standard C languages, and has improved some features to circumvent common code errors. The assignment operator (=) has no return value, so that the assignment operator (=) is misused when the equals operator (= =) is used. Arithmetic operators (+,-,*,/,%, and so on) detect and block value overruns, avoiding the range of values of the storage type (larger or smaller than the actual result, or imprecise--joe.huang) at the time of the operation. If you need to support overflow behavior, you can choose Swift's overflow operator, where details are visible in the overflow operator (translated later).



Unlike the C language, Swift allows for the remainder (%) of floating-point numbers. Swift also provides two interval operators not available in the C language (a.. B and A...b), as a convenient notation for the range of domain.



This chapter describes the common operators in Swift. The Advanced Operators (translated later) chapter covers the advanced operators in Swift, and explains how to customize the operators and let the standard operators support the operations of custom types.



Operator terminology



The operators are divided into one-dollar, two-yuan, ternary operators:



· The unary operator (unary operator) operates on a single target (such as-a). The unary operator prefix (unary prefix operator) immediately precedes the operation target (such as!b), while the unary operator suffix (unary postfix operator) follows the operation target (for example, i++).



· The binary operator (binary operator) operates on two targets (such as 2 + 3), which belong to the infix (infix) operator, because (the operator symbol) appears between two operational targets.



· The ternary operator (ternary operator) operates on three targets. Like the C language, Swift has only one ternary operator: the ternary conditional operator (that is, a?). B:C).



The value of an operator operation is called an operand (operands). In expression 1 + 2, the + symbol is the two-dollar operator, and its two operands are the value 1 and the value 2.



Assignment operator



The assignment operator (Assignment operator,a = b) Initializes or updates A's value with the value of B:



1 Let B = 2 var a = 5 3 A = B 4//A are now equal to 5//a equals 10



If the right side of the assignment statement is a tuple that contains multiple values, the elements of the tuple can be extracted at once with multiple constants or variable decomposition (the method mentioned in the previous chapter about the decomposition tuple value--joe.huang):



1 let (x, y) = (1, 2) 2//X are equal to 1, and y are equal to 2 3//x equals 1,y equals 2



Unlike assignment operators in the C or Objective-c languages, the assignment operator of the Swift language does not itself return a value. Therefore, the following statement is incorrect:



1 if x = y {2//This isn't valid, because x = y does not return a value 3//It is invalid because x = y does not returns a value (X==y can, Joe.huang) 4}



This attribute avoids the misuse of the assignment operator (=) when the equals operator (= =) is used. By denying the validity of if x = y, Swift will help you circumvent such errors in your code.



Arithmetic operators



Swift supports the use of four standard arithmetic operators for all numeric types:



· Add: +



· Minus:-



· By: *



· In addition to:/



1 1 + 2//equals 3 2 5-3//equals 2 3 2 * 3//equals 6 4 10.0/2.5//equals 4.0



Unlike arithmetic operators in the C/objective-c language, Swift's arithmetic operators do not allow value overruns by default. If you need to support overflow behavior, you can choose Swift's overflow operator (for example, a &+ b), where details are visible in the overflow operator (translated later).



The plus operator also supports String concatenation:



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



You can add two Character (character, Unicode character--joe.huang) values, or add a Character value to a string value to get a new string value:



1let Dog:character = ""



2 Let Cow:character = ""



3 Let Dogcow = dog + Cow



4//dogcow equals ""



See Character and string concatenation (translated in later chapters):



Remainder operator



The remainder operator (remainder operator, a% b) to find out how many b A contains, and return the remaining values (that is, the remainder after the divide Remainder).



Note:



The remainder operator (%) is also called the modulo operator (modulo operator) in other languages. However, the results of the calculation of the negative numbers show that the implementation of Swift language is a strict residual operation, rather than a model-seeking operation.



The principle of the remainder operator is as follows. To calculate 9 4, first ask for the number of 4 contained in 9:






As shown in the figure, 9 contains two 4, and the remainder is 1 (the orange portion).



The wording of Swift is as follows:


9% 4//equals 1

 




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.