- 1、本文档共54页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
04周-第4-5章-顺序结构与选择结构剖析
形式2:if-else语句 求两数最大最小值 int x, y, max, min; if (x y) { max = x; min = y; } else { max=y; min=x; } * 多条语句时{}千万不能省略 if (x y) max = x; min = y; else max=y; min=x; 形式2:if-else语句 求两数最大最小值 int x, y, max, min; if (x y){ max = x; min = y; }else{ max=y; min=x; } * int a,b,c, m; // 求a,b,c中最大值,存在m中 if (ab){ if (a c) m = a; else m = c; }else{ if (b c) m = b; else m = c; } if (ab) if (a c) m = a; else m = c; else if (b c) m = b; else m = c; 形式3:if嵌套 * else与if的配对规则: 与上面的、 离它最近的、 且在同一复合语句中 还没有配对的if子语配对。 if-else 配对 if (ab) { if (ac) if (ad) flag=1; else flag=2;} else flag=3; * else与if的配对规则: 与上面的、 离它最近的、 且在同一复合语句中 还没有配对的if子语配对。 int a,b,c, m; // 求a,b,c中最大值,存在m中 if (ab ac) m = a; else if (ba bc) m = b; else m = c; 形式3:if嵌套 * else与if的配对规则: 与上面的、 离它最近的、 且在同一复合语句中 还没有配对的if子语配对。 示范问题 #1 问题1(输出前后字母):给定任意一个英文字母,输出其在英文字母表中的前一个和后一个字母,如 输入 a,输出 z, b * 上节课的实现 #include stdio.h #include stdlib.h int main() { char c, p, n; scanf(%c, c); p = (c==a) ? z : ((c==A) ? Z : c-1); n = (c==z) ? a : ((c==Z) ? A : c+1); printf(%c, %c\n, p, n); system(pause); } * 这节课用if-else #include stdio.h #include stdlib.h int main() { char c, p, n; scanf(%c, c); p = (c==a) ? z : ((c==A) ? Z : c-1); if (c== a) p = z; else if (c==A) p = Z; else p=c-1; printf(%c, %c\n, p, n); system(pause); } * 这节课用if-else #include stdio.h #include stdlib.h int main() { char c, p, n; scanf(%c, c); if (c== a) p = z; else if (c==A) p = Z; else p=c-1; if (c== z) n = a; else if (c==Z) n = A; else n=c+1; printf(%c, %c\n, p, n); system(pause); } * 示范问题 #2 问题2(判闰年):输入年份,输出该年是否是闰年 如输入 1900,输出 0 闰年规则? 四年一闰,百年不闰,四百年再闰 * 开始程序填空 #include stdio.h #include stdlib.h int main() { int x; scanf(%d, x); // 四年一闰 if (x % 4==0) printf(1\n); else printf(0\n); system(pause); } * 开始程序填空 #include stdio.h #include stdlib.h int main() { int x; scanf(%d, x); // 四年一闰 +
文档评论(0)