zoukankan      html  css  js  c++  java
  • javascript语法之循环语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript"> 
     
    /*
    if 
     
    switch
     
    switch语句的特殊之处: case后面可以跟常量与变量或者是表达式。
     
    循环语句:
    	
    	while
    		
    		格式:
    			while(判断的条件){
    				循环体内容	
    			}
    			
     
     
     
    	var count = 0 ; 
    	while(count<5){
    		document.write("hello world<br/>");
    		count++;
    	}
     
    需求: 计算1~100的总和。
     
    	var num = 1;
    	var result = 0;
    	while(num<=100){
    		result +=num;	
    		num++;
    	}
    	
    	document.write("结果:"+result);
     
    do-while循环语句
     
    	do{
    		循环语句;
    	}while(判断条件);
    	
    需求;计算1~100奇数的总和。
     
    	var num = 1;
    	var result = 0;
    	do{
    		if(num%2!=0){
    			result +=num;
    		}
    		num++;
    	}while(num<=100);
     
    	document.write("结果:"+result);
     
     
    for循环语句:
    	
    	格式:
    		for(初始化语句; 判断的条件 ; 循环后的语句){
    			循环体语句;	
    		}
     
     
    需求;计算1~100偶数的总和。
    */	
    	
    	var result = 0 ; 	
    	for(var i =  1 ; i<=100 ; i++){
    		if(i%2==0){
    			result +=i;
    		}
    	}	
    	
    	document.write("结果:"+result);
    		
    </script>
     
     
     
     
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
     
    <body>
    </body>
    </html>
    

  • 相关阅读:
    Idea快捷键
    Java学习之路--书籍推荐
    泵式等待基元
    uni-app,wex5,APPcan,ApiCloud几款国内webapp开发框架的选型对比
    前端框架2019 云开发
    select2 javascript控件 如何设置指定的值
    Github 索引
    linux
    WPF 中的 Uri 地址的不同写法
    WPF GridSplitter 使用技巧
  • 原文地址:https://www.cnblogs.com/wanghang/p/6299791.html
Copyright © 2011-2022 走看看