Swift Fundamentals of Process Control

Source: Internet
Author: User

? Introduction to Swift Process Control

? Swift provides a C-like process control structure that includes a for and while loops that can perform tasks multiple times.

Select if and switch statements that perform different code branches based on specific criteria, and control flow jumps to other code

The break and continue statements.

In addition to the traditional for conditional increment (for-condition-increment) loop in C, Swift also increases

Added a for-in loop to make it easier to iterate over the group (array), the Dictionary (dictionary), the interval

(range), string, and other sequence types.

? Swift's switch statement is more powerful than the C language. In the C language, if a case is accidentally leaked

Write a break, this case will run through (Fallthrough) to the next Case,swift no need to write break,

So this cross-cutting situation does not occur. Case can also match more type patterns, including interval matching

(range matching), tuple (tuple), and specific type.

? For loop

A For loop is used to execute a series of statements multiple times as specified

? Swift provides two kinds of for-loop form: for-in, for-conditional increments

? for condition Increment (for-condition-increment)

? used to repeatedly execute a series of statements until certain conditions are reached, typically by increasing the count after each loop is completed

Value of the device to implement the

? for-in

? for-in is used to traverse an interval range (range), sequence (sequence), set

(collection), all elements of the series (progression)

? If you don't need to know the value of each item in the interval, you can use an underscore (_) to override the variable name to ignore the

Access to values

? While loop

? Swift provides two form of while loop: while, Do-while

? while

? First judge the condition, then execute

? do-while

Do it first, then judge the condition.

? conditional statements

? Swift provides two types of conditional statements: The IF statement and the switch statement

? Use an If statement when the condition is simple and the possible situation is rare. And the switch statement is more suitable for conditions than

Complex, more likely, and need for pattern matching (pattern-matching)

? If

The simplest form of an if statement is to include only one condition, and only if the condition is true does the relevant

Code that executes the ELSE statement when the condition is false

Let today's Day?? Good =true

If today's Day?? Good

println ("We're going to climb??")

}else {

println ("Learning in the Classroom")

}

? Switch

The switch statement attempts to match a value to a number of patterns (pattern). According to the first match to a

mode, the switch statement executes the corresponding code.

When it is possible to use a switch statement to replace the IF statement

The switch statement consists of multiple case, and each case is a branch of code execution

? In some cases where it is not possible to cover all values, you can use the default branch to satisfy this requirement, which

A default branch must be in the last face of the switch statement

Let Chara:character = "A"

Switchchara {

Case "A", "a"://If you want to match multiple conditions, you can do so in the?? A case???? Put multiple conditions?? (,) separated

println ("The Letter A")//each?? A case score?? Must contain?? Less?? -Line Statement

Case "A":

println ("The Letter A")

Default

println ("Default")

}

There is no implicit penetration (no implicit fallthrough)

? Switch in Swift, when the code in the matching case branch finishes executing, the program terminates the switch language

Continuation of the next case branch. This means that you do not need to explicitly make the

Use the break statement

? Each case branch must contain at least one statement; If you want to match multiple conditions, you can

The polygon separates multiple conditions with commas

Every?? A case score?? Must contain?? Less?? If you want to match multiple conditions, you can A case???? Put multiple conditions??

(,) separated

Let Anothercharacter:character = "a"

Switchanothercharacter {

Case "a"://Compile Error

Case "A":

println ("The Letter A")

Default

println ("Not the letter A")

}

Interval range Matching (range Matching)

? The pattern of a case branch can also be a range of values

Make?? Range match to output any number corresponding to?? The language?? Format

Let Count =3_000_000_000_000

Let countedthings = "stars in the Milky"

var naturalcount:string

Switchcount {

CASE0:

Naturalcount = "No"

Case1 ... 3:

Naturalcount = "a few"

Case4 ... 9:

Naturalcount = "several"

Case10 ... 99:

Naturalcount = "tens of"

Case100 ... 999:

Naturalcount = "hundreds of"

case1000 ... 999_999:

Naturalcount = "thousands of"

Default

Naturalcount = "millions and millions of"

}

println ("There are\ (Naturalcount) \ (countedthings).")

? Match tuple tuple

? You can use tuples to test multiple values in the same switch statement. An element in a tuple can be a value, or it can be a

Range. Also, use an underscore (_) to match all possible values

? Swift allows multiple case to match the same value (C language is not supported). If there are multiple matches, only the first

A case branch that is matched to

Make???? A tuple of type (int, int) to classify the points in (x, y)

Let Somepoint = (a)

Switchsomepoint {

Case (0,0):

println ("(0, 0) is at the origin")

Case (_,0):

println ("(\ (somepoint.0), 0) is on the x-axis")

Case (0,_):

println ("(0,\ (Somepoint.1)) is on the y-axis")

Case ( -2...2, -2...2):

println ("(\ (somepoint.0), \ (Somepoint.1)) is inside the box")

Default

println ("(\ (somepoint.0), \ (Somepoint.1)) is outside of the box")

}

? value binding (Values Bindings)

The case branch allows matching values to be bound to a temporary constant or variable in which the case

Can be referenced in a branch-this behavior is called value binding

In?? A tuple of type (int, int). Values are bound to the points in the classification (x, y)

Let Anotherpoint = (2,0)

Switchanotherpoint {

Case (Let x,0)://At this time x is just?? A placeholder,?? To temporarily get the switch condition in the?? One or more values

println ("On the x-axis with an X value of\ (x)")

Case (0,let y):

println ("On the y-axis with a Y value of\ (y)")

Caselet (x, y):

println ("somewhere else at (\ (x), \ (y))")

}

? Where Append condition

The case branch can use the where statement to determine additional conditions

Case points?? Can make?? Where statement to determine the additional condition

Let Yetanotherpoint = (1,-1)

Switchyetanotherpoint {

Caselet (x, y) where x = = y://assigns the value to X, Y, and requires that X. equals y

println ("(\ (x), \ (y)) is on the line x = = y")

Caselet (x, y) where x = =-Y:

println ("(\ (x), \ (y)) is on the line x = = y")

Caselet (x, y):

println ("(\ (x), \ (y)) is just some arbitrary point")

}

? control pass-through statements (controlled Transfer statements)

The control transfer statement changes the order in which you execute the code, through which you can implement the code jump

? Swift has four kinds of control transfer statements:

? continue

? break

? fallthrough

? return

? Continue

The continue statement tells a loop to stop the loop iteration and start the next loop iteration again.

Continue statement tell?? A loop body?? A stop?? This iteration of the loop starts the next iteration of the loop again. Just fine.

Like saying, "This iteration of the loop I've been practicing?" Are you done? Next?? Time. "

Let Puzzleinput = "great minds think alike"

var puzzleoutput = ""

For Characterinpuzzleinput {

Switch character {

Case "A", "E", "I", "O", "U", "":

Continue

Default

Puzzleoutput + = character

}

}

println (Puzzleoutput)

? Break

The break statement immediately ends the execution of the entire control flow. When you want to end a switch code block earlier or

A loop body, you can use the break statement

? Break in a looping statement

? When a break is used in a loop body, the execution of the loop body is immediately interrupted and then jumps to the presentation loop

The first line of code after the body ends with a curly brace (}). No more code for this loop iteration will be executed, no more

The next iteration of the loop is generated.

? Break in a Switch statement

? When a break is used in a switch code block, execution of the switch code block is immediately interrupted, and the

Go to the first line of code after the curly brace (}) that represents the end of the switch code block.

? label statement (labeled statements)

? You can use tags to mark a loop body or switch code block, and when you use break or continue,

With this tag, you can control that the tag represents the interrupt or execution of the object and is suitable for complex control flow structures

According to the score rating, over 100 points skipped, encountered negative stop?? Cycle

var score = [96,83,43,101,66,70, -5,99]

!

First:for Sinscore {//define label first

Switch S/10 {

Case10:

Continue first//make?? Label

CASE9:

println ("\ (s) divided into excellent")

Case8:

println ("\ (s) divided into good")

Case6 ... 7:

println ("\ (s) divided into medium")

CASE0:

Break first//make?? Label, end?? For loop. What if this?? Did not make?? tab, the break will be a switch

Default

println ("\ (s) divided into failed")

}

}

Swift Fundamentals of Process Control

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.