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?