布尔变量的用处
之前看到过布尔变量,当时不明白这有什么作用,看到“条件判断”后,才知道它可以用在这里。又在CSDN上逛了逛,原来逻辑运算的结果也是一个布尔值。
/*使用布尔变量进行条件判断*/
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int age;
printf("Input your age:");
scanf("%d",&age);
bool age_check = age <= 18;
if (age_check)
printf("Please come in!");
else
printf("You are not allowed to enter the pub.");
return 0;
}
就目前来看,我还是觉得用常规的表达方式比较直观,可能bool有更好的用处,我还没遇到。
/*常规的条件判断*/
#include <stdio.h>
int main(void)
{
int age;
printf("Input your age:");
scanf("%d",&age);
if (age >= 18)
printf("Please come in!");
else
printf("You are not allowed to enter the pub.");
return 0;
}
评论