Swift Chinese Course (iii) Process Control

Source: Internet
Author: User
Tags case statement range

Swift uses the IF and switch to write conditional control statements and write loops with For-in,for,while and do-while. In conditional control statements and circular statements, parentheses are optional, but curly braces are required to enclose the loop body:

Let Individualscores = [$, $, $, $]  
var teamscore = 0 for  
score in Individualscores {  
    if score > 5 0 {  
        Teamscore + = 3  
    } else {  
        Teamscore = 1  
    }  
}  
Teamscore

If statement

In the IF statement, the condition must be a Boolean value, that is, if score {...} This conditional sentence is wrong, not a condition that is implicitly compared to 0.

You can combine if and let to prevent loss of this value. These values mean optional, this optional value either contains a specific value or contains a nil (null) to indicate that its value does not exist. After the type of the value is added? Number to indicate that it is optional:

var optionalstring:string? = "Hello"
optionalstring = = Nil  
       
var optionalname:string? = "John Appleseed"
var greeting = "Hello!"
If let name = optionalname {  
    greeting = "Hello, \ (name)"
}

Practice:

Assign the Optionalname to nil. What's going to happen? Add an else conditional clause to try setting a different value when Optionalname is nil.

Once the optional value is nil and false, the code block in the curly braces is skipped, otherwise the optional value is not encapsulated and assigned to a constant, which makes the encapsulated value visible in the code block scope.

Switch statement

The switch statement supports any data type and supports multiple comparisons, not limited to integer or equality tests:

Let vegetable = ' red pepper '
switch vegetable {case  
' celery ': let  
    vegetablecomment = "Add some raisins and MA Ke ants on a log. "
Case "cucumber" and "watercress": let  
    vegetablecomment = "This would make a good tea sandwich."
Case Let X where X.hassuffix ("Pepper"): let  
    vegetablecomment = "is it a spicy \ (x)?"
Default: let  
    vegetablecomment = "Everything tastes good in soup."
}

Practice:

Remove the default statement and see what's wrong with the report?

After you execute the case statement that satisfies the condition, the program jumps out automatically, so you don't need to break each case statement.

For-in statement

When traversing in a dictionary using the for-in loop, a pair of names is provided to use each name value pair in the dictionary (Number--numbers of the following example):

Let interestingnumbers = [  
    "Prime": [2, 3, 5, 7, one],  
    "Fibonacci": [1, 1, 2, 3, 5, 8],  
    "Square": [1, 4, 9, ,  
]  
var largest = 0 for  
(kind, numbers) in Interestingnumbers {to number in  
    numbers {  
        if num ber > Largest {  
            largest = number  
        }}}  
largest

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Practice:

In the example above, add a variable to track which number is the largest, that is, the maximum value?

While statement

Use the while statement to repeatedly execute a block of code until the condition changes. The condition can be placed at the end of the sentence so that the code executes at least once:

var n = 2 while  
N < MB {  
    n = n * 2  
}  
n  
       
var m = 2 do  
{  
    m = m * 2  
} while M <  
m

For statement

You can use the index value in the For loop to use the. (Two dots) or explicitly declaring an initial value, conditional clause or increment to limit the range of the index, the following two loops mean the same thing:

var firstforloop = 0 for  
i in 0..3 {  
    Firstforloop + = i  
}  
firstforloop  
       
var secondforloop = 0 for  
va R i = 0; I < 3; ++i {  
    Secondforloop = 1  
}  
secondforloop

Use.. (Two dots) limits the range of the index and ignores the highest value, with the ... The range of (three-point) constructs contains two values.

Author: cnblogs Joe.huang

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.