天天看點

JavaScript-do while 循環

while 循環的格式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        while (條件表達式){
            需要重複執行的代碼;
        }
    </script>
</head>
<body>
</body>
</html>      

while 循環的特點:隻有條件表達式為真,才會執行循環體。

do while 循環的格式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        do {
            需要重複執行的代碼;
        } while (條件表達式);
    </script>
</head>
<body>
</body>
</html>      

do while 循環的特點:無論條件表達式是否為真,循環體都會被執行一次。

while 循環示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        while (false) {
            console.log("www.it6666.top");
        }
    </script>
</head>
<body>
</body>
</html>      
JavaScript-do while 循環

do while 循環示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Demo</title>
    <script type="text/javascript">
        do {
            console.log("www.it6666.top");
        } while (false);
    </script>
</head>
<body>
</body>
</html>      
JavaScript-do while 循環

通過如上的兩個示例可觀察出這兩個循環的不同特點。

繼續閱讀