
上QQ阅读APP看书,第一时间看更新
5-4 if … else语句
程序设计时更常用的功能是条件判断为True时执行某一个程序代码区块,当条件判断为False时执行另一段程序代码区块,此时可以使用if … else语句,它的语法格式如下:
if (条件判断):# 小括号可以省略 程序代码区块一 else: 程序代码区块二
上述观念是如果条件判断是True,则执行程序代码区块一,如果条件判断是False,则执行程序代码区块二。可以用下列流程图说明这个if … else语句:

程序实例ch5_3.py:重新设计ch5_1.py,多了年龄满20岁时的输出。

执行结果

程序实例ch5_4.py:奇数偶数的判断。

执行结果
