Python中的if和while
if 判断
if形式有三种:
1、if ...
2、if ... else ...
3、if ... elif ... else ...
实例:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 inp = raw_input('>>>') 2 if inp=='1': 3 print '111' 4 elif inp =='2': 5 print '222' 6 else: 7 print '...'
注意:缩进为四个空格或一个Tab键
while循环
格式:
1 while 条件: 2 # 循环体 3 # 条件为真的话,执行循环体 4 # 条件为假的话,跳出循环
break
break用于退出所有循环
例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 n1=1 2 while n1 < 3: 3 if n1 ==2: 4 break 5 n1=n1+1 6 print n1,'break'
输出结果:
2 break
continue
continue用于退出本次循环,继续本次循环
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 n1=1 2 while n1<=3: 3 print '123' 4 n1 = n1 + 1 5 continue 6 print '456'
输出结果:
123
123
123