1、控制流简介
控制流是控制语句、语句块、存储过程的执行分流。
2、OC 中有哪些控制流
1.if 语句 / 2.if...else... 语句 / 3.switch 语句
4.while 语句 / 5.do...while... 语句 / 6.Bool 布尔值
3、OC 中控制流的含义
1》if 语句的含义
if(object){
NSLog("A");
};
NSLog("B");
含义:如果object(对象)存在,即判断条件成立。则就执行NSLog("A") 语句; 否则,不执行 NSLog("A") 语句,按代码从上到下的指定循序指定下面 NSLog("B") 语句。
2》if...else... 语句的含义
if(object){
NSLog("A");
}else{
NSLog("B");
}
含义:如果object(对象)存在,即判断条件成立。则就执行NSLog("A") 语句; 否则,执行 NSLog("B") 语句。
3》switch 语句的含义
switch(value) {
case pattern:{
code1
}
break;
case pattern1:{
code2
}
break;
default:{
code3
}
}
含义:value 在 case 中查找对应的数值,即 value==pattern || pattern1。找到对应的数值,这执行对应数值下方{}里面的代码 code1 || code2。否则,将执行default对应的 code3 代码。
4》while 语句含义
while (condition) {
code
}
含义:先判断condition 是否为真,如果condition为真,则执行code代码,在判断 condition 是否还为真,如果为真则执行 code 代码,否则,跳出循环。如果 condition 一直为真,这一直循环下去,直到condition 条件为假,跳出循环。
5》do...while... 语句的含义
do {
code
} while (condition);
含义:首先执行 code 代码,在判断condition 条件是否成立,如果为真,则继续执行 code 代码;为假则跳出循环。
6》BOOL 布尔值
YES || NO
4、Swift 中的控制流
1》if 语句
/**
if 语句
特点: 不需要将条件判断语句写在()里面,但是条件成立时要执行的代码必须用{}包括住。
*/
if 1+1 == 2 {
print("检验通过")
}
2》 if...else... 语句
/**
if...else...
特点:同if语句
*/
if 1+2 == 4 {
print("检验通过")
}else{
print("检验未通过")
}
3》while 语句
/**
while 语句
特点:判断条件不写在()里面。先判断,在执行
*/
var temp = 0
while temp<10 {
temp += 1
}
print(temp)
4》 repeat... whlie... 语句
/**
repeat...while... 语句 (do...while...)
特点:先执行,在判断
*/
var num = 0
repeat {
num += 1
}while num<6
print(num)
5》switch 语句
/**
switch 语句
特点:根据给定的参数,在下面 case 中,匹配对应的参数,在执行对应参数下的代码,要break 结尾
*/
switch 9 {
case 1:
break
case 6:
break
case 9:
print("匹配成功")
break
default:
break
}
6》布尔值
/**
true || false 布尔值
*/
let isBool:Bool = true
print(isBool)
网友评论