大一/c语言/选择

if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
类型1

if(case A){
//一条语句不用括号
}
else if(case B){
//若一条符合则不会看后面的语句
}
else{

}

类型2

if(case 1)
if(case 2)//每条都会看

判断条件

表达式复杂勤用括号

三目运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 if(A) B
else C

可写成
A?B:C//c++中唯一一个三目运算符
//conditional expression

abs(int a)
=>(n < 0) ? -n : n

//多个 ‘?’
int i=(num<10)?5:
(num>50)?10:
(num<50 && num>10)?20:30;
//循序渐进满足?否则执行后面的语句
int num=41
i=20

switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
switch(expression){//A 为一个常量表达式
case A: ~~~(break);
case B: ~~~(break);
default://都不符合
}

int main(){
char a;
cin>>a;
switch (a)
{
case 'a':
printf("is a\n");
break;
default:
cout<<"not a"<<endl;
break;
}
}//A 为一个常量表达式

If the case label matches the switch’s expression, then the statements that follow that label are executed up until the break statement is encountered.

若没有break,一直执行,直到break

Shiwei Pan wechat