逻辑类型和运算
bool 类型
#include<stdbool.h>
ture false
1 真 0 假
逻辑运算
! 逻辑非 有0则为1 有1则为0
&& 逻辑与 a&&b 有0则0 两个1才为1 (并且)
|| 逻辑或 a||b 有1则1(或者) 两个0才为0
! >&&>||
6>x&&x>4 开区间 6>=x&&x>=4 闭区间
条件运算符
条件?条件满足时候的条件:条件不满足时候的条件
其他运算符>条件运算符>赋值运算符
嵌套条件表达式
不建议使用
逗号运算符 主要是在for中使用
找出三个数中的最大数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int max = 0;
if (a > b) {
if (a > c) {
max = a;
}
else {
max = c;
}
}
else {
if (b > c) {
max = b;
}
else {
max = c;
}
}
printf("This max is %d\n。", max);
}
分段函数
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int x;
scanf("%d", &x);
int f = 0;
if (x < 0) {
f = -1;
}else if (x == 0) {
f = 0;
}else {
f = 2*x;
}
printf("%d", f);
return 0;
}
多路分支
swith case
控制表达式 只是整数型的结果
常量可以是常数 也可以是常数的计算表达式
case 与其常量表达式必须用空格隔开
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
int type;
scanf("%d", &type);
switch ( type ) {
case 1:
printf("你好!\n");
break;
case 2:
printf("早上好!\n");
break;
case 3:
printf("中午好!\n");
break;
case 4:
printf("晚上好!\n");
break;
default:
printf("你在说什么!\n");
}
return 0;
}