Swift 2.0 Learning Note (Day 18)-How many branch statements?

Source: Internet
Author: User

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

Branch statements are also called conditional statements, and the SWIFT programming language provides three branch statements of if, switch, and guard.

if Statement

The choice structure guided by the IF statement has the IF structure, the IF-ELSE structure and the ELSE-IF structure 3 kinds.

The example code for the If structure is as follows:

var score = 95

If score >= 85 {

Print ("You're excellent!") ")

}

The results of the program run as follows:

You are so good!

The sample code for the IF-ELSE structure is as follows:

var score = 95

If score < 60 {

Print ("Failed")

} else {

Print ("Pass")

}

The results of the program run as follows:

Pass

The sample code for the ELSE-IF structure is as follows:

Let Testscore = 76

var grade:character

If Testscore >= 90 {

Grade = "A"

} else if Testscore >= 80 {

Grade = "B"

} else if Testscore >= 70 {

Grade = "C"

} else if Testscore >= 60 {

Grade = "D"

} else {

Grade = "F"

}

Print ("Grade = \ (Grade)")

The output results are as follows:

Grade = C

Switch Statement

The switch statement is also called a toggle statement, which provides a multi-branch program structure.

The example code for the basic form of the switch statement is as follows:

Let Testscore = 86

var grade:character

Switch TESTSCORE/10 {

Case 9:

Grade = "excellent"

Case 8:

Grade = "Liang"

Case 7, 6:

Grade = "Medium"

Default

Grade = "Poor"

}

Print ("Grade = \ (Grade)")

The output results are as follows:

Grade = Benign

Guard Statement

The Guard statement is a newly added keyword for Swift 2.0, which is very similar to the IF statement and is designed to improve the readability of the program.

The guard statement must have an else statement with the following syntax:

Guard conditional expression-else {

Jump statement

}

Statement Group

When the conditional expression is true, skips the contents of the Else statement and executes the statement group contents. The conditional expression is false when the content in the Else statement is executed, and the jump statement is typically return, break, continue, and throw.

Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.

Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com

Luxgen Classroom Forum Website: http://51work6.com/forum.php

Swift 2.0 Learning Note (Day 18)-How many branch statements?

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.