6.Part 6
6.1 Switch语句
#include
int main(void)
{unsigned age;printf("Please input a number:(tupe integer):\n");int result = scanf_s("%3u",&age);if (result == 0) {printf("请输入合法数据!\n");return 0;}switch (age) {case 40:printf("恭喜!中奖了,四十岁大礼包!赠送房子!\n");break;case 50:printf("恭喜!中奖了,五十岁大礼包!奖励苹果!\n");break;case 70:printf("恭喜!退休!Goodbye\n");break;default:printf("没找到您的年龄,可能您的年龄没有准备礼包\n");break;}return 0;
}
6.2 ?:三元运算符
#include
int main(void)
{int number = 9;(number == 10) ? printf("没毛病!\n") : printf("有问题!\n");//如果()内判断为真,则执行:前的语句,如果()内判断为假,则执行:后的语句return 0;
}
6.3 char型变量
#include
int main(void)
{char character = 'A';//注意单个字节的符号用单引号表示printf("character = %c\n", character);return 0;
}
7.1 ASCII 码
计算机内部无法储存字符,所有字符都是对应一个int型整数存储在计算机内部的。
#include
int main(void)
{char character = 'A';//注意单个字节的符号用单引号表示printf("character = %d\n", character);return 0;
}
8.1 while循环
#include
#define ONE_KILOMETER 1000
int main(void)
{int run_meter &#61; 0;while (run_meter <&#61; ONE_KILOMETER) {printf("running...%d\n", run_meter);run_meter&#43;&#43;;}printf("我他妈终于跑完了&#xff01;\n");
}
continue&#xff1a;跳过本次循环&#xff0c;继续执行下一次循环。
#include
#define GOLD 1000
int main(void)
{int rush &#61; 1;while (rush <&#61; GOLD) {if (rush &#61;&#61; 500) {printf("挖到铁矿&#xff01;&#xff01;&#xff01;扔掉不要&#xff01;\n");rush&#43;&#43;;//注意不要漏掉自增变量这条语句&#xff0c;否则将一直执行if语句continue;}printf("rush &#61; %d\n", rush);rush&#43;&#43;;}printf("挖完了&#xff01;\n");return 0;
}
8.2 do while循环
注意&#xff1a;do while循环语句最后有分号&#xff0c;而while循环语句最后没有分号。
先执行do循环中的内容&#xff0c;后判断&#xff0c;判断结果如果是true&#xff0c;那么继续执行do中的代码块
#include
#define GOLD 1000
int main(void)
{int rush &#61; 1;do {rush&#43;&#43;;printf("rush &#61; %d\n", rush);} while (rush <&#61; 1000);return 0;
}