//
// c语言基础,分支结构
//BOOL,布尔类型,是一个非真即假的数据类型,只有两个值YES和NO
//YES = 1,NO = 0;
//在C语言中,非0即为真;(-1、3都是真)
int a = 0;
BOOL b= NO;
//关系运算符
int m = 5, n = 10;
BOOL result = NO;
//大于
result = m > n;
printf("result = %d\n", result);//BOOL类型用%d打印 no
//大于等于
result = m >= n;
printf("result = %d\n", result); // no
//小于 <
result = m < n;
printf("result = %d\n", result); //yes
//小于等于 <=
result = m <= n;
printf("result = %d\n", result); //yes
//等于 ==
result = m == n;
printf("result = %d\n", result); //no
//不等于 !=
result = m != n;
printf("result = %d\n", result); //yes
//关系运算符左右都是表达式
result = (m + n) > 10;
//逻辑运算符
int i = 3, j = 5, k = 7;
//逻辑与 &&
//&&左右两侧 同时为真才为真,有一个为假即为假
//相当于并且
result = i >= j && k != j; //no
//逻辑或 ||
//||左右两侧,同时为假才为假,有一个为真即为真
//相当于“或者”
result = j == i || j < k; //yes
//逻辑非 !
//颠倒黑白
result = !(i != k); //no
//逻辑短路
//1.逻辑与得短路状态
//当&&左侧的结果为假时,右侧就不参与判断
int c = 0;
result = i > k && c++;
printf("i = %d, k = %d, c = %d\n", i, k, c);
//2.逻辑获得短路状态
//当||左侧的结果为真时,右侧就不参与判断
result = (i + j) >= k || c++;
printf("i = %d, k = %d, c = %d\n", i, j, k);
//github 程序员要求的或者博客
//cocoaChina
//if语句
//第一种形式
/*
if (条件语句) {
语句1;
…;
}
int count = 10;
int flag = 0;
printf("是否看到西瓜(1、是;2、否):");
scanf("%d", &flag);
if (flag == 1) {
count = 1;
}
printf("买了%d个包子\n",count);
int grade;
printf("请输入考试分数:");
scanf("%d", &grade);
if (grade == 100) {
printf("么么哒!亲一个。\n");
}
else if (60 <= grade) {
printf("怎么回事,下次加油\n");
}
else {
printf("跪下!!!\n");
}
char gender;
printf("请输入一个字符:");
scanf("%c", &gender);
if (gender == ‘m‘ ) {
printf("男性\n");
}
if(gender != ‘m‘ ){
printf("女性\n");
}
*/
//if的第二种形式
/*
if ( 条件表达式){
语句1;
}else{
语句2;
}
char gender;
printf("请输入一个字符:");
scanf("%c", &gender);
if (gender == ‘m‘ ) {
printf("男性\n");
}
else{
printf("女性\n");
}
int a = 0, b = 0, max = 0;
printf("请输入两个整数:");
scanf("%d %d", &a, &b);
if ( a > b ) {
printf("max = %d\n", a);
} else
{
printf("max = %d\n", b);
}
int age = 0;
printf("美女你年龄是多少?");
scanf("%d", &age);
if (20 <= age && age <= 30 ) {
printf("咱们交往吧!\n");
} else {
int height = 0;
printf("输入身高:");
scanf("%d" , &height);
if (height >= 165) {
printf("身高还是可以的\n");
}
else {
printf("我去接孩子放学\n");
}
}
int year;
printf("请输入年份:");
scanf("%d", & year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
printf("是闰年\n");
} else {
printf("是平年\n");
}
//3.if的第三种形式
if (条件语句1) {
语句1;
} else if (条件语句2){
语句2;
} else {
语句3;
}
float grade = 0.0;
printf("请输入你的成绩:");
scanf("%f", &grade);
if (grade >90 && grade <= 100) { //已经说明它小于90,就是已经排除了上面的条件
printf("你获得“优”\n");
} else if (grade >= 80) {
printf("你获得“良”\n");
} else if (grade >= 70) {
printf("你获得“中”\n");
} else if (grade >= 60) {
printf("你获得“差”\n");
} else {
printf("你不及格\n");
}
char string;
printf("请输入一个字符:");
scanf("%c", &string);
if (string >=‘0‘ && string <= ‘9‘ ) { //还可以通过ASCII表查询其范围进行断
printf("this is digital\n");
} else if (string >= ‘A‘ && string <= ‘Z‘ ) {
printf("this is capital letter\n");
} else if (string >=‘a‘ && string <= ‘z‘ ) {
printf("this is letter\n");
} else {
printf("other\n");
}
// getchar(); 获取缓冲区的一个字符
// rewind(stdin) 清空缓冲区
char c = 0;
printf("请输入一个字符:");
scanf("%c", &c);
char c1 = getchar();
rewind(stdin);
char d = 0;
printf("请输入一个字符:");
scanf("%c", &d);
printf("c = %c, c1 = %c, d = %c\n", c, c1, d );
//条件运算符(三目运算)
// 条件表达式 ? 表达式1 : 表达式2;
int num1 = 20, num2 = 10;
int max = 0;
//max = num1 > num2 ? num1 : num2;
//相当于
if (num1 > num2 ) {
max = num1;
} else {
max = num2;
}
printf("max = %d\n", max);
int num1 = 0, num2 = 0, max = 0;
printf("请输入两个整数:");
scanf("%d %d", &num1, &num2);
max = num1 >= num2 ? num1++ : ++num2;
printf("max = %d, num1 = %d, num2 = %d\n", max, num1, num2);
//switch
switch (整型表达式) {
case 值1 :
语句1;
break;
case 值2;
语句2;
break;
case…;
default:
break;
}
int count = 0;
int price = 0;
printf("输入买酸奶瓶数:");
scanf("%d", &count);
switch (count) {
case 1 :
price = 3;
break;
case 3 :
price = 10;
break;
default:
price = 3 * count;
break;
}
printf("price = %d\n", price);
//switch的执行顺序;
//根据整型表达式的返回值,去找对应的case
//如果找到,就执行case中的语句,直到遇到break才跳出switch语句
//如果找不到,就执行default中的语句
//switch需要注意
//1.case中加break;
//2.不要忘了写default;
int mouth = 0, day = 0;
printf("请输入月份:");
scanf("%d", &mouth);
switch (mouth) {
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
day = 31;
break;
case 2 :
day = 28;
break;
case 4 : case 6 : case 9 : case 11 :
day = 30;
break;
default:
printf("输入月份不对\n");
break;
}
printf("该月的天数为:%d\n", day);
return 0;
}