//逻辑操作符 &&(逻辑与) ||(逻辑或)
int main()
{
int a = 3;
int b = 5;
int c = 0;
int d = 0;
printf("%d\n", a&&b); // 1 &&两个数都非0 结果为1,否则为0
printf("%d\n", a&&c); // 0
printf("%d\n", a || b);//1 ||两个数至少一个非0,结果为1;全是0,结果为0
printf("%d\n", a || c);//1
printf("%d\n", d || c);//0
system("pause");
return 0;
}
int main()
{
int i = 0;
int a = 0, b = 2, c = 3, d = 4;
//i = a++&&++b&&d++;
i=a++||++b||d++;
//printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);//1 2 3 4 若&&前为假(0),后面不用算;前面为真,后面要算
printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);// 1 3 3 4 若||前为真,后面不用算;前面为假,后面要算
system("pause");
return 0;
}
//条件操作符 exp1? exp2:exp3 如果exp1为真 ,执行exp2;若为假,执行exp3
int main()
{
int a = 0;
int b = 0;
//a > 5 ? b = 3 : b = -3;
b=a > 5 ? 3 : -3;
printf("%d\n", b);
rerurn 0;
}
//逗号表达式,从左向右一次执行。整个程序的结果是逗号最后一个表达式结果
int main()
{
int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);
printf("c=%d\n", c);//13
}
int main()
{
int a = 3;
int b = 5;
int c = 0;
int d = 0;
printf("%d\n", a&&b); // 1 &&两个数都非0 结果为1,否则为0
printf("%d\n", a&&c); // 0
printf("%d\n", a || b);//1 ||两个数至少一个非0,结果为1;全是0,结果为0
printf("%d\n", a || c);//1
printf("%d\n", d || c);//0
system("pause");
return 0;
}
int main()
{
int i = 0;
int a = 0, b = 2, c = 3, d = 4;
//i = a++&&++b&&d++;
i=a++||++b||d++;
//printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);//1 2 3 4 若&&前为假(0),后面不用算;前面为真,后面要算
printf("a=%d\nb=%d\nc=%d\nd=%d\n", a, b, c, d);// 1 3 3 4 若||前为真,后面不用算;前面为假,后面要算
system("pause");
return 0;
}
//条件操作符 exp1? exp2:exp3 如果exp1为真 ,执行exp2;若为假,执行exp3
int main()
{
int a = 0;
int b = 0;
//a > 5 ? b = 3 : b = -3;
b=a > 5 ? 3 : -3;
printf("%d\n", b);
rerurn 0;
}
//逗号表达式,从左向右一次执行。整个程序的结果是逗号最后一个表达式结果
int main()
{
int a = 1;
int b = 2;
int c = (a > b, a = b + 10, a, b = a + 1);
printf("c=%d\n", c);//13
}