[譯]Javascript中的do-while循環
阿新 • • 發佈:2017-10-18
www. java target invalid spa say write cas 菜單
本文翻譯youtube上的up主kudvenkat的javascript tutorial播放單
源地址在此:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
在本次視頻中我們會用例子來討論一下Javascript中的do-while循環,在16章中我們討論了while循環
while循環:
1.while循環首先檢查條件
2.如果條件為真,則循環中的語句執行
3.這個順序一直重復直到條件為假
do-while循環:
1.do-while循環在循環的最後檢查條件
2.這就意味著do-while循環能保證至少循環能執行一次
3.總的來說,do-while循環是用來給用戶展示一個菜單用的
while循環和do-while循環的區別:
1.while循環在開頭檢查條件,do-while循環在循環的結尾檢查條件
2.do-while循環能保證運行至少一次,但是while循環則不然
do-while循環例子:
var userChoice = ""; do { var targetNumber = Number(prompt("Please enter your target number", "")); var start = 0; while (start [= targetNumber) { document.write(start+ "[br/]"); start = start + 2; } do { userChoice = prompt("Do you want to continue - Yes or No").toUpperCase(); if (userChoice != "YES" && userChoice != "NO") { alert("Invalid choice. Please say, Yes or No"); } } while (userChoice != "YES" && userChoice != "NO"); }while (userChoice == "YES");
[譯]Javascript中的do-while循環