标签:
BOOL是一个非真即假的数据类型
BOOL为布尔型,只有一个字节,取值false和true,是0和1的区别
BOOL flag = YES,m=NO;//值有YES和NO
printf("%d\n",flag);//YES: NO;
printf("%d\n",m);//YES:1,NO:0
C语言中,规定非0即为真
flag = 0;//假
flag = 100;//真
flag = -1;//真
关系运算符
int a = 10,b = 5;
// 大于.
BOOL c =a>b;//YES
// 小于<
BOOL d = a<b;//NO
// 大于等于
BOOL f = a<b;//YES
// 小于等于<=
BOOL e = a<b;//NO
// 等于==
BOOL g = a ==b;//NO
// 不等于!=
BOOL h= a!=b;//NO
// 关系运算符两侧的都是表达式
BOOL i=a+b>10;
int a = 3,b = 5,c = 7;
逻辑与&&
全真为真,有一个为假则为假,相当于"并且"
BOOL d = a <= b && c == b;
逻辑或||
BOOL e = b >= c || a < b;
||两侧表达式全假即为假,有一个为真则为真
相当于"或者"
逻辑非!
黑白颠倒
逻辑与的短路状态
当&&左侧的表达式 结果为假的时候,右侧就不参与判断
逻辑或的短路状态
当||左侧的表达式结果为真的时候,右侧就不参与判断了
BOOL f = !(a!=b);
int count = 10;
BOOL flag = NO;
printf("是否看见卖西瓜的");
scanf("%d%d",&c,&f);
if (count==1)
写法一
int score = 100;
printf("今天考了多少分");
scanf("%d",&score);
if (score==100)
printf("宝贝!亲一个");
写法2
int score = 100;
printf("今天考了多少分");
scanf("%d",&score);
if (score==100)
printf("宝贝!亲一个");
if
char a = 0;
printf("填写下划线部分:I Love __" );
scanf("%c",&a);
if(a == ‘u‘|| a ==‘U‘);
printf("开始恋爱");
if (a != ‘u‘|| a == ‘U‘);
printf("不谈");
int a = 0;
printf("请输入你的年龄:\n");
scanf("%d",a);
if (a<=24&&a>=18){
printf("开始恋爱");
}else{
printf("不谈\n");
}
空语句
int a=0,b=0;
printf("请输入2个数字:");
scanf("%d %d",&a,&b);
if (a>b){
printf("max=%d\n",a);
}
if(a<=b){ printf("max=%d\n",b);
}
max= a;
int year=0;
printf("输入一个年份:");
scanf("%d",&year);//大键盘的回车结束
if ((year=year%4==0 && year%100!=0)||year%400==0)
{
printf(" %d年份是闰年\n",year);
}else{
printf(" %d年份是平年\n",year);
int age= 0,height=0;
printf("请输入你的年龄和身高厘米:\n");
scanf("%d %d",&age,&height);
if( (age<=24 && age>=18) && height>=160)
{
printf("你的电话号码是:\n");
}else{
printf("不好意思,我结婚了!\n");
int age= 0;
printf("请输入你的年龄:\n");
scanf("%d",&age);
if (age<=24 && age>=18)
{
printf("年量满足\n");
}else{
printf("年龄不足!\n");
int height=0;
printf("身高是:");
scanf("%d",&height);
if(height>=160){
printf("身高满足");
}else{printf("身高不满足,以后见\n");
}
float score=0.0;
printf("请输入您的成绩:");
scanf("%f",&score);
if(score>=90){
printf("优\n");
}else if (score>=80 ){
printf("良\n");
}else if(score>=70 ){
printf("中\n");
}else if(score>=60){
printf("及格\n");}
else {
printf("不及格\n");
}
char m=0;
printf("请输入一个字符:");
scanf("%c",&m);
if(m>=‘0‘&& m<=‘9‘){
printf("%c是数字\n", m);
}else if(m>=‘A‘&&m<=‘Z‘){
printf("%c是大写字母\n", m);
}else if(m>=‘a‘&&m<=‘z‘){
printf("%c是小写字母\n",m);
}else {
printf("%c是其他字符\n",m);
}
条件运算符?:
int a = 10, b =20,max = 0;
max = a > b? a:b;
printf("max=%d\n",max);
max = a>b? ++a:b++;
printf("max=%d a = %d b = %d\n",max,a,b);
int price = 0,count = 0;
printf("请输入酸奶的数量:\n");
scanf(" %d",&count);
if (count==1) {
printf("price=3");
}
switch (count) {//整型表达式(返回值是整型数)
case 1://必须用常量
{
price = 3;
break;//不要忘记写break
}
case 3:
{ price = 10;
break;
}
default:
{
price =3*count;
break;
}
printf("买酸奶花了多少钱:%d\n",price);
int season = 0;
printf("请输入您的数字:\n");
scanf("%d",&season);
switch (season) {//整型表达式(返回值是整型数)
case 1:
{
printf("%d是春天\n",season);
break;//不要忘记写break
}
case 2:
{ printf("%d是夏天\n",season);
break;
}
case 3:
{ printf("%d是秋天\n",season);
break;
}
case 4:
{ printf("%d是冬天\n",season);
break;
}
default:
{ printf("%d是未知季节\n",season);
break;
}
}
int month=0,day = 0;
printf("请输入您喜欢的月份:");
scanf("%d",&month);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
day =31;
break;
}
case 4:
case 6:
case 9:
case 11:
{
day = 30;
break;
}
case 2:
{
day = 28;
break;
}
}
if(month>=1&& month<=12)
{
printf("%d月有%d天\n",month,day);
}else
{
printf("输入错误");
标签:
原文地址:http://www.cnblogs.com/xiangrongsu/p/4308769.html