厦门公司做网站,wordpress交易,展示型网站怎么做,网页升级紧急通知中由于本人有一定的编程基础#xff0c;因此会简略基础的语法的介绍#xff0c;主要整理的是与python语法有所不同的部分。分支语句主要分为两部分#xff1a;if语句和switch语句#xff0c;也会介绍条件操作符#xff08;三目运算符#xff09;1.if语句基本格式#xff1…由于本人有一定的编程基础因此会简略基础的语法的介绍主要整理的是与python语法有所不同的部分。分支语句主要分为两部分if语句和switch语句也会介绍条件操作符三目运算符1.if语句基本格式if (expression statement else if(expression)//if条件不成立 statement else//上面两个条件表达式都不成立 statement例如如下代码#define _CRT_SECURE_NO_WARNINGS 1 #includestdio.h /* Read A and print whether A is negative, positive or zero. 1 a is positive -1 a is negative 0 a is zero */ int main() { int a; scanf(%d, a); if (a 0) printf(a is negative\n); else if (a 0) printf(a is positive\n); else printf(a is zero\n); return 0; }如果if语句之后需要执行多句语句则需要用大括号把语句都括起来否则就始终会执行。#define _CRT_SECURE_NO_WARNINGS 1 #includestdio.h int main() { int a; scanf(%d, a); if (a 0) printf(a is negative\n); printf(This line is always printed\n); else if (a 0) { printf(a is positive\n); printf(This line is not always printed\n); } else printf(a is zero\n); return 0; }if语句可以嵌套#define _CRT_SECURE_NO_WARNINGS 1 #includestdio.h /* Read As age and print whether A is a child or an adult. 18 a is an adult 5 a is a child 0 a is a child -3 Error: Age cannot be negative. */ int main() { int a; scanf(%d, a); if (a 0) { if (a 18) { printf(a is a child\n); } else { printf(a is an adult\n); } } else printf(Error: Age cannot be negative.\n); return 0; }悬空else的匹配原则由于C语言对缩进没有硬性要求所以else会和上文最近的if语句匹配而不是同一缩进的语句。#define _CRT_SECURE_NO_WARNINGS 1 #includestdio.h int main() { int a; scanf(%d, a); if (a 0) if (a 18) printf(a is a child\n); else printf(Error: Age cannot be negative.\n); return 0; } /* 18 Error: Age cannot be negative. */因为此时else匹配的if是if (a 18)。如果有大括号就不用考虑这个问题了else不会冲进大括号里匹配if的。2.switch语句多条分支相当于把多个else if简略了。基本用法switch (expression) { case value1: statement case value2: statement ... default: statement }会根据expression的值执行不同的case分支如果输入的值都不符合case的条件可以按照需求选择是否使用default语句。但和else语句不同的是default语句的位置并没有要求放在switch语句最前面也是可以的。需要注意的是expression语句必须是整型表达式。case和后面的数字之间要有空格。即case 0而不是case0 例#include stdio.h int main() { int n 0; scanf(%d, n); switch(n % 3) { case 0: printf(整除余数为0\n); break; case 1: printf(余数是1\n); break; case 2: printf(余数是2\n); break; default: printf(Error); } return 0; } /* 3 整除余数为0 4 余数是1 5 余数是2 */但是如果删掉break的话代码会继续往下执行可能会执行其他case语句的代码比如我删掉break之后输入1结果如下利用这个性质我们还可以对不同分支的相同输出作简化。比如如果要判断今天是不是周末可以这么写#define _CRT_SECURE_NO_WARNINGS 1 #include stdio.h int main() { int day 0; scanf(%d, day); switch (day) { case 1: case 2: case 3: case 4: case 5: printf(工作日\n); break; case 6: case 7: printf(周末\n); break; } return 0; } /* 3 工作日 6 周末 */这是因为case 3条件满足之后会一路往下执行然后就会把case 5的语句执行了因此1到5都有共同的输出值。3.条件操作符也叫三目表达式形式为expression1?expression2:expression3例如#define _CRT_SECURE_NO_WARNINGS 1 #include stdio.h int main() { int a 0; int b 0; b (a ? 3 : 5); printf(%d %d, a, b); return 0; } /* 0 5 */三目操作符也可以嵌套#define _CRT_SECURE_NO_WARNINGS 1 #include stdio.h int main() { int a, b, c, max; scanf(%d %d %d, a, b, c); max (a b) ? ((a c) ? a : c) : ((b c) ? b : c); printf(max %d, max); return 0; } /* 3 2 1 max 3 1 2 3 max 3 */虽然很简洁但是会降低代码的可读性慎重使用。在输出的时候也可以用例如printf(The string \%s\ is %s\n, str, (strcmp(str, Hello) 0) ? a greeting : not a greeting);三目运算符给我一种lambda函数的感觉把几行的def语句压缩成一行。下期讲循环结构。