分支语句
1. 条件语句 if-else ,基本语法形式如下,其中 “[]” 中 的部分可以省略。
- if boolean-expression {
- statement1;
- } [else if boolean-expression {
- statement2;
- } ] [else {
- statement3;
- } ]
实例:
- var number1 = 0;
- var number2 = 1;
- var max = 0;
- if number1 >number2 {
- max = number1;
- } else if number1 < number2 {
- max = number2;
- } else {
- max = number1;
- }
- println("Themaximum is \(max)")
2 .多分支语句 switch 它的语法形式如下:
- switch some value to consider {
- case value 1:
- respond to value 1 case value 2, value 3:
- respond to value 2 or 3
- default:
- otherwise, do something else
- }
每个 case 不需要显式地添加 break ,每个 case 至少有 一条语句。
可以比较任何类型。
实例1:
- let someCharacter: Character ="e" switch someCharacter {
- case "a", "e", "i","o", "u":
- println("\(someCharacter) isa vowel")case "b", "c", "d", "f", "g", "h", "j", "k", "l","m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
- println("\(someCharacter) is a consonant")
- default:
- println("\(someCharacter) is not a vowel or a consonant")
- }
实例2:
- let count = 3_000
- let countedThings = "stars inthe Milky Way"varnaturalCount: String
- switch count {
- case 0:
- naturalCount ="no" case 1...3:
- naturalCount ="a few" case 4...9:
- naturalCount ="several"
- case 10...99:
- naturalCount ="tens of" case 100...999:
- naturalCount ="hundreds of" case 1000...999_999:
- naturalCount ="thousands of"
- default:
- naturalCount = "millions and millions of"
- }
- println("There are \(naturalCount) \(countedThings).")
循环语句
while语句
do-while语句
for语句
for in语句
while 循环
- while condition{
- statements
- }
- vari = 100 varr = 0 vars= 0 vart = 0
- while i < 1000 {
- r= i / 100
- s=( i - r* 100 ) / 10
- t= i - r* 100 - s* 10
- if( i == r * r* r + s* s *s + t* t * t){
- border-style: none none none solid; border-left-width: 3px; border-left-color: #6ce26c; color: inherit; line-height: 18px; margin-bottom: 0px !important; margin-left: 0px !important; padding-right: 3px !importa
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接