/*循環
while循環異常
格式:while(循環保持條件){須要執行的語句}while
OC:
int i = 0;
int sum = 0;
while (i <= 10) {
sum = i++;
}
while (i <= 10)
sum = i++;
NSLog(@"%d", sum);
若是隻有一條指令while後面的大括號能夠省略
Swift:
0.while後的圓括號能夠省略
1.只能以bool做爲條件語句
2.若是隻有條指令while後面的大括號不能夠省略
*/
var i:Int = 0
var sum:Int = 0
while (i <= 5)
{
i += 1
sum = i
}
print("\(sum)")
var i1:Int = 0
var sum1:Int = 0
while i1 <= 10
{
i1 += 1
sum1 = i1
}
print(sum1)
/*
do while循環
格式:do while(循環保持條件) {須要執行的語句}
OC:
int i = 0;
int sum = 0;
do {
sum = i++;
} while (i <= 10);
NSLog(@"%d", sum);
int i = 0;
int sum = 0;
do
sum = i++;
while (i <= 10);
NSLog(@"%d", sum);
若是隻有一條指令if後面的大括號能夠省略
Swift2.0以後變爲 repeat while, do用於捕捉異常
0.while後的圓括號能夠省略
1.只能以bool做爲條件語句
2.若是隻有條指令do後面的大括號不能夠省略
*/
var i2:Int = 0
var sum2:Int = 0
repeat{
i2 += 1
sum2 = i2
}while(i2 <= 10)
print(sum2)
var i3:Int = 0
var sum3:Int = 0
repeat{
i3 += 1
sum3 = i3
}while i3 <= 10
print(sum3)