while 循环 输出100以内的数、和、奇数(do while)

左先生 2020-09-30 PM 265℃ 0条

while 100以内的和:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while 循环 输出100以内的数、和、奇数</title>
</head>
<body>
    <script type="text/javascript">
        var i=0;
        while(i<100){
            document.write(i+'<br />');
            i++;
        }

    </script>
</body>
</html>

00742-dq6yeaqdm5j.png

100以内的奇数:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while 循环 输出100以内的数、和、奇数</title>
</head>
<body>
    <script type="text/javascript">
        var i=0;
        while(i<100){
            if(i%2!=0){
                document.write(i+'<br />');
            }
            i++;
        }

    </script>
</body>
</html>

100以内数的和(不包括100)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>while 循环 输出100以内的数、和、奇数</title>
</head>
<body>
    <script type="text/javascript">
        var i=0;
        var num=0;
        while(i<100){
            num+=i;
            i++;
        }
        document.write(num);
    </script>
</body>
</html>

do while 的使用(语法中会先执行 do中语法):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>do while 的使用</title>
</head>
<body>
    <script type="text/javascript">
        var i=0;
        do{
            document.write(i+'<br/>');
            i++
        }while(i<100);
    </script>
</body>
</html>

注意:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>do while 的使用</title>
</head>
<body>
    <script type="text/javascript">
        var i=101;
        do{
            document.write(i+'<br/>');
            i++
        }while(i<100);
        document.write(i+'<br/>');
    </script>
</body>
</html>

00322-a8q0kxbc39p.png

标签: while, do while

非特殊说明,本博所有文章均为博主原创。

评论啦~