python循環判斷語法 while... else,  for... else

while… else 是python的一種循環判斷語法

就是在循環的同時做判斷,很驚詫python居然有這種很爽的語法

count = 0 
while count < 5: 
	print count, " is less than 5" 
	count = count + 1 
else: 
	print count, " is not less than 5"

如果是java的話,就得這樣來寫了,還得判斷結束時機

count = 0 
while (true){
	count = count + 1 
	if (count < 5) {
		print count, " is less than 5" 
	} else {
		print count, " is not less than 5"
		return;
	}
}

同理,for… else也是,這裏不再贅述,如果有其他特殊情況沒有考慮到,歡迎留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章