[Swift learning] Swift programming (4) Basic operators and swift Operators

Source: Internet
Author: User

[Swift learning] Swift programming (4) Basic operators and swift Operators

Swift supports most operators in the Standard C language and improves many features to reduce common coding errors. for example, if the value assignment operator = does not return a value, it will prevent the Bug from writing equal sign = as a value =. numeric operators (+,-, *,/, %, and so on) detect non-allowed value overflow, to avoid the exception caused by the variable being greater than or less than the range that its type can carry. of course, you can choose to use the Swift overflow operator to play overflow. for more information, see the overflow operator. different from the C language, in Swift, you can perform the remainder operation (%) on floating point numbers, and provide the interval operator that does not exist in the C language to express the values between two numbers (.. B and... b). This facilitates us to express the values in a range.

 

I. Assignment Operator)

The value assignment operator (a = B) initializes or updates the value of a using the value of B.

let b = 10var a = 5a = b

If the right side is a combination of values, its elements can be immediately divided into multiple constants or variable values.

let (x,y) = (1, 2)

 

Unlike C and Objective-C, the Swift assignment operation does not return any value. Therefore, the following expression is incorrect:

if x = y {    // this is not valid, because x = y does not return a value}

 

This feature makes it impossible to write the = Error as =, because if x = y is an error code, Swift helps you avoid these code errors from the underlying layer.

 

Ii. Numeric Operators

Swift supports four arithmetic operations for all numeric types: + ,-,*,/

 

Iii. remainder Operators

% Is the remainder operator, and the extra part is called the remainder. The return result of positive and negative numbers is the same. Different from C and OC. Swift supports the remainder of floating point numbers.

 

Iv. Auto-increment and auto-subtraction Operators

Like C, Swift also provides operators that allow you to add one or minus one to the variable itself. The operation object can be an integer or floating point type. Bytes

 

5. interval Operators

Swift provides two operators to easily express the value of a range.

1. Closed Interval Operator

Closed Interval operator... B defines an interval that contains all values from a to B (including a and B. the distinct closed interval operator is very useful when all values in one interval are iterated.

for index in 1...5 {   println("\(index) * 5 = \(index * 5)") } 

 

 

2. semi-closed interval Operator

The semi-closed interval operator is very useful when you traverse arrays.

Let names = ["Anna", "Alex", "Brian", "Jack"] let count = names. count for I in 0 .. count {println ("the \ (I + 1) Name \ (names [I])")}

 

 

6. logical operators

Non-logical! A logic and a & B logic or a | B

 

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.