swift-2-Control Flow

Source: Internet
Author: User

Control flow

Use if and switch as conditional controls. Use For-in, for, while, and do-while as loops. The parentheses are not required, but the main braces are required.

Let individualscores = [75, 43, 103, 87, 12]

var Teamscore = 0

For score in individualscores{

If score > 50{

Teamscore + = 3

}else{

Teamscore + = 1

}

}

Teamscore

In an if statement, the condition must be a Boolean expression, which means that if score {...} is wrong and cannot be suppressed compared to 0.

You can use if and let together to prevent the loss of values. These values are optional. An optional value can contain a value or contain a nil to specify that the value does not already exist. Write a question mark "?" After the type indicates that the value is optional.

var optionalstring:string? = "Hello"

Optionalstring = Nil

var optionalname:string? = "John Appleseed"

var greeting = "Hello!"

If let name = optionalname {

Greeting = "Hello, \ (name)"

}

If the optional value is nil, the condition is false the code in the curly braces is skipped. Otherwise, the optional value is not wrapped and assigned to a constant, which will be the non-wrapped value of the variable into the code block

Switch supports multiple data and multiple comparisons, without limiting the need to be integers and test equality.

Let vegetable = "red pepper"

Switch Vegetable {

Case "Celery":

Let vegetablecomment = "Add some raisins and make ants on a log."

Case "cucumber", "watercress":

Let vegetablecomment = "so would make a good tea sandwich."

Case Let X where X.hassuffix ("Pepper"):

Let vegetablecomment = "is it a spicy \ (x)?"

Default://by Gashero

Let vegetablecomment = "everything tastes good in soup."

}

After the match is performed, the program jumps out of the switch instead of continuing to the next situation. So you no longer need break to jump out of switch .

You can use for-in to iterate through each element in the dictionary, providing a pair of names to use each key-value pair.

Let interestingnumbers = [

"Prime": [2, 3, 5, 7, 11, 13],

"Fibonacci": [1, 1, 2, 3, 5, 8],

"Square": [1, 4, 9, 16, 25],

]

var largest = 0

For (kind, numbers) in interestingnumbers{

For number in numbers {

If number > largest

{

largest = number

}

}

}

Use the while to repeat the code block until the condition changes. The condition of the loop can be placed at the end to ensure that the loop executes at least once.

var n = 2

While n < 100

{

n = n * 2

}

N

var m = 2

do {

m = m * 2

}

While M < 100

M

You can keep an index in the loop that represents the index range or explicitly declares an initial value, condition, increment. The two loops do the same thing:

var firstforloop = 0

For I in 0..3 {

Firstforloop + = i

}

Firstforloop

var secondforloop = 0

for var i = 0; I < 3; ++i {

Secondforloop + = 1

}

Secondforloop

Use.. The constructed range ignores the highest value and uses ... The constructed range contains two values.

swift-2-Control Flow

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.