JavaScript中的for循环

for循环

格式:

for(变量初始化 ; 条件判断 ;  变量更新)

{

        循环体代码;

}

5.png

实例:求100以内能被10整除的数字的和

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var sum = 0; 
            for(var i=1;i<=100;i++){
                if(i%10==0){
                    sum+=i;
                }
            }
            document.write("100以内能被10整除的数之和为:"+sum)
        </script>
    </head>
    <body>
    </body>
</html>


Weiter lernen
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var sum = 0; for(var i=1;i<=100;i++){ if(i%10==0){ sum+=i; } } document.write("100以内能被10整除的数之和为:"+sum) </script> </head> <body> </body> </html>
einreichenCode zurücksetzen