Welcome to Swift (initial translation and annotations of Apple's official Swift document 17)---108~115 page (chapter II)

Source: Internet
Author: User

Range Operators (range operator)

There are two range operators in Swift, both of which are an abbreviation for a numeric range expression.

Closed range Operator (closed interval range operator)

The Closed interval range operator (a...b) definition declares a range from a to B, and includes A and B.

The closed interval operator is typically used to iterate over all values within a range, such as the for-in loop:

For index in 1...5 {

println ("\ (index) times 5 is \ (Index * 5)")

}

1 times 5 is 5

2 times 5 is 10

3 times 5 is 15

4 times 5 is 20

5 times 5 is 25

Half-closed Range Operator (semi-closed range operator)

Semi-closed range operator (a). b) The definition declares a range from a to B, but does not include the value of B. That is, the semi-closed range operator contains the first value a of the range, but does not contain the last value B.

The semi-closed range operator is particularly useful when dealing with a zero-based list (for example, a value), which can be used to calculate the length of the list (but not over the length):

Let names = ["Anna", "Alex", "Brian", "Jack"]

Let count = Names.count

For I in 0..count {

println ("Person \ (i + 1) is called \ (Names[i])")

}

Person 1 is called Anna

Person 2 is called Alex

Person 3 is called Brian

Person 4 is called Jack

Note that the number contains four elements in the code, but the 0..count will only be up to 3 (the largest index of the numeric element), due to the semi-closed range operation.

Logical Operators (logical operator)

The logical operator modifies or combines the values of Boolean types with True and false.

Swift supports three basic logical operators in C language:

      • Logical not (!A)//logical Non-
      • Logical and (a && B)//Logic and
      • Logical or (a | | b)//logic or

Logical not Operator (logical)

The logical non-operator (!a) Reverses the value of the Boolean type, turning true to false, or false to true.

The logical operator is a prefix operator that is written in front of the action value without any spaces. You can understand it as "not ...", refer to the following code example:

Let Allowedentry = False

If!allowedentry {

println ("ACCESS DENIED")

}

Prints "ACCESS DENIED"

The statement if!allowedentry can be understood as "if not allowed entry. (If entry is not allowed). If the condition is true, the subsequent statement executes, that is, the value of Allowedentry is false.

In this code example, be aware of the use of Boolean-type constants and the names of variables, which helps to make the code more readable and accurate, and avoids the use of double negation or confusing logical sentences.

Logical and Operator (logic and)

The logic and operators (a && b) create a logical expression, and the value of the entire expression is true only if the value of each expression is true.

If the logical and any of the values are false, then the value of the entire expression is false. In fact, if the first value of the logic is false, then the second expression will not be evaluated because it is not possible to make the entire expression a value of true. This is the short-circuit diagnosis (short-circuit evaluation: the author according to his understanding of the translation of this professional terminology, may be different from other materials)

In the following example, a value of two Boolean types is determined, and only two values are true to allow access:

Let Entereddoorcode = True

Let Passedretinascan = False

If Entereddoorcode && Passedretinascan {

println ("welcome!")

} else {

println ("ACCESS DENIED")

}

Prints "ACCESS DENIED"

Logical or Operator (logical OR)

The logical OR operator (a | | b) is an infix operation, using | | Represents a logical expression that is built with it: as long as two values have a value of true, the value of the entire expression is also true.

The value of the expression is confirmed with logic similar to logic or using short-circuit diagnostics. If the logical or left expression value is true, then the expression on the right does not participate in the operation, because it is not possible to change the result of an entire expression.

In the following code example, the first Boolean value (Hasdoorkey) is false, but the second value (Knowsoverridepassword) is true, because as long as a value is true, the value of the entire expression is true, so access is possible:

Let Hasdoorkey = False

Let Knowsoverridepassword = True

If Hasdoorkey | | Knowsoverridepassword {

println ("welcome!")

} else {

println ("ACCESS DENIED")

}

Prints "welcome!"

Combining Logical Operators (compound logic operator)

You can use the compound logic operator to create a long, complex logical expression:

If Entereddoorcode && Passedretinascan | | Hasdoorkey | | Knowsoverridepassword {

println ("welcome!")

} else {

println ("ACCESS DENIED")

}

Prints "welcome!"

In this example, a number of && and | |  operator to create a long, complex expression. however && | | The operator still handles only two values, so it can actually be understood to be a combination of three small expressions:

If the correct gate number (Entereddoorcode) is entered, and a retinal scan (Passedretinascan) is passed, or if there is a door key (Hasdoorkey), or if the password for the emergency channel is known ( Knowsoverridepassword), then you can pass.

Depending on the values of Entereddoorcode, Passedretinascan and Hasdoorkey, it is possible to know that the result of the preceding two expressions is false, but the emergency channel password is known (Knowsoverridepassword = True), So the value of the entire expression is true.

Explicit parentheses (Clear brackets limit)

Sometimes, wrapping expressions with parentheses () can make complex expressions more readable (although this limitation is not mandatory in swift). In the preceding code, you can add parentheses () to make the composite expression more explicit:

if (entereddoorcode && passedretinascan) | | Hasdoorkey | | Knowsoverridepassword {

println ("welcome!")

} else {

println ("ACCESS DENIED")

}

Prints "welcome!"

Parentheses make the previous two values more explicit, separating as much as possible in the entire expression, without affecting the value of the entire expression, and making the entire expression easier to read. In code, readability is always higher than simplicity.

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.