python-004(while循环)

 1 案例一:
 2 ks = 1
 3 while True:
 4     print(ks)
 5     if ks == 10:
 6         break
 7     ks = ks + 1
 8 print("end")
 9 
10 
11 
12 案例二:
13 while True:
14     print('123')
15     break
16     print('456')
17     
18     
19 while True:
20     print('123')
21     continue
22     print('456')
23 
24 # break:用于跳出当前循环,并且brrak下面的代码,将不再执行。
25 # continue: 用于跳出本次循环,继续下一次循环。
26 
27 案例三:
28 # 求1-100的所有数的和
29 start = 1
30 
31 while True:
32     print(start)
33     if start == 100:
34         break
35     start +=1
36 
37 案例四:
38 # 输出 1-100内所有的偶数
39 start = 1
40 
41 while start <= 100:
42     if start%2 == 0:
43         print(start)
44     start += 1
45         
46 案例五:
47 # 输出1-100 内所有的奇数
48 start = 1
49 
50 while start <= 100:
51     if start%2 == 1:
52         print(start)
53     start += 1
54 
55 案例六:
56 # 打印1,2,3,4,5,6,8,9,10 
57 start =1 
58 while True:
59     if start == 7:
60         start += 1
61         continue
62     print(start)
63     if start == 10:
64         break
65     #start = start + 1
66     start +=1

 

posted @ 2020-05-09 23:33  haven_rui  阅读(97)  评论(0编辑  收藏  举报