// main.swift swift
// swift流程控制 數組
// 函數
// Created by zhangbiao on 14-6-13. 測試
// Copyright (c) 2014年 理想. All rights reserved. spa
// .net
import Foundation code
// swift 流程控制 blog
/* 生命週期
swift 流程控制主要分爲: 順序 分支 循環 字符串
1.順序:從上到下依次執行,遇到函數調用,調用完畢函數繼續執行
2.分支:根據分支條件進行分支
(1)if{} else{}
(2)switch {case:...}
3.循環:循環執行,遇到結束條件
(1)while 和 do{} while
(2) for in 和 for(){}
4.循環分支中,控制流程的四個關鍵字 continue break fallthrough return(多用於函數中) 相關說明
*/
func testfunc()
{
//1.順序(略)---------------------------------------------------------------------------------------
//2.分支--------------------------------------------------------------------------------------------
//聲明一個Bool 類型的變量 賦值爲true
var bgen:Bool = true
//分支
if(bgen)
{
println(bgen);
}else
{
println(bgen);
}
//分支 switch {case:...}
/*
特色:(1)支持任意類型的數據以及各類比較操做(不單單是整數以及測試相等)
(2)運行switch中匹配到的子句以後,程序會退出switch語句 ,並不會繼續向下運行,因此不須要在每一個子句結尾寫break 若是想繼續執行 在原來break 的位置寫 fallthrough 便可
*/
////switch ()中的值能夠是Int
var value=123;
switch(value)
{
case 123:
println("1")
fallthrough; //繼續執行
case 2:
println("2")
case 3:
println("3")
default:
println("沒有匹配的")
}
//switch ()中的值能夠是字符串
switch("理想")
{
case "理想":
println("理想")
case "理想2":
println("理想2")
case "理想3":
println("理想3")
default:
println("沒有匹配的字符")
}
//case 中能夠有多個匹配項
switch("abc")
{
case "123":
println("123");
case "456","abc":
println("123 abc ");
default:
println("沒有找到合適的匹配");
}
// 比較操做 hasSuffix 函數是判斷字符字符串是否是以其參數結尾
switch("理想 and swift")
{
case let x where x.hasSuffix("swift"): // 注意此時的 x 的值就是switch()中的值where 額外的判斷條件
println("swift");
case "理想":
println("理想");
default:
println("me");
}
//2.循環--------------------------------------------------------------------------------------------
//循環
//while(){} 和 do{}while() 重複運行一段代碼直到不知足條件。循環條件能夠在開頭也能夠在結尾
var i:Int = 0;
while( i<10)
{
i++;
println(i);
}
do
{
i--;
println(i);
}while(i>0);
//for in
//使用for-in循環來遍歷一個集合裏面的全部元素,例如由數字表示的區間、數組中的元素、字符串中的字符
for index in 1...5
{
println("index=\(index)");
}
//解釋... 區間的意思 1...5 的意思 [1,5] 1..5 的意思 [1,5) index是一個每次循環遍歷開始時被自動賦值的常量。這種狀況下,index在使用前不須要聲明,只須要將它包含在循環的聲明中,就能夠對其進行隱式聲明,而無需使用let關鍵字聲明。 index常量只存在於循環的生命週期裏。若是你想在循環完成後訪問index的值,又或者想讓index成爲一個變量而不是常量,你必須在循環以前本身進行聲明。
//若是你不須要知道區間內每一項的值,你可使用下劃線(_)替代變量名來忽略對值的訪問
var num=0;
for _ in 1...5
{
num++;
println("num =\(num)");
}
//遍歷字符
for str in "ABCDE"
{
println("str=\(str)");
}
//for(){ }
for(var i=0; i<10; i++)
{
println("i=\(i)");
}
//4.循環分支中,控制流程的四個關鍵字說明--------------------------------------------------------------------------------------------
//continue語句告訴一個循環體馬上中止本次循環迭代,從新開始下次循環迭代。就好像在說「本次循環迭代我已經執行完了」,可是並不會離開整個循環體。
//break語句會馬上結束整個控制流的執行。當你想要更早的結束一個switch代碼塊或者一個循環體時,你均可以使用break語句。當在一個switch代碼塊中使用break時,會當即中斷該switch代碼塊的執行,而且跳轉到表示switch代碼塊結束的大括號(})後的第一行代碼。
//fallthrough fallthrough關鍵字不會檢查它下一個將會落入執行的 case 中的匹配條件。fallthrough簡單地使代碼執行繼續鏈接到下一個 case 中的執行代碼,這和 C 語言標準中的switch語句特性是同樣的
//return 在函數中具體講
}
// 調用函數
testfunc();
//執行結果:
true
1
2
理想
123 abc
swift
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
0
index=1
index=2
index=3
index=4
index=5
num =1
num =2
num =3
num =4
num =5
str=A
str=B
str=C
str=D
str=E
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
Program ended with exit code: 0