java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)
本文知识点(目录):
1、while循环语句
2、do...while循环语句
3、for循环语句
4、for...in循环语句
5、附录1(with语句)
6、附录2(打印多边形及乘法表)
1、while循环语句
格式:
while(判断条件){
循环体内容代码;
}
1.1、实例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>while循环练习</title>
</head>
<script type="text/javascript">
/*
循环语句:while循环
格式:
while(判断条件){
循环体内容代码;
}
*/
//打印5次helloWorld
var a=0;
while(a<5){
document.write("helloWorld</br>");
a++;
}
//需求计算1-100的总和
var num=1;
var sum=0;
while(num<=100){
sum+=num;
num++;
}
document.write(sum+"</br>");
</script>
<body>
</body>
</html>
实例结果图
2、do...while循环语句
格式:
do{
循环体内容代码;
}while(判断条件)
2.1、实例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script type="text/javascript">
/*
循环语句:do...while循环
格式:
do{
循环体内容代码;
}while(判断条件);
*/ //需求计算1-100种中奇数的和
var b=1;
var sum=0;
do{
if(b%2!=0){
sum+=b;
}
b++;
}while(b<=100);
document.write(sum+"</br>"); </script>
<body>
</body>
</html>
实例结果图
3、for循环语句
格式:
for(初始化语句;判断条件;循环后的语句){
循环语句代码;
}
3.1、实例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>for循环练习</title>
</head>
<script type="text/javascript">
/*
循环语句:for循环
格式:
for(初始化语句;判断条件;循环后的语句){
循环语句代码;
}
*/ //计算1-100的偶数和
var sum=0;
for(var c=1;c<=100;c++){
if(c%2==0){
sum+=c;
}
}
document.write(sum);
</script>
<body>
</body>
</html>
实例结果图
4、for...in循环语句
格式:
for(var 变量名 in 要遍历的目标变量名){ }
4.1、for...in语句的作用
1.可以用于遍历数组的元素。 注意:使用for-in语句遍历数组元素时遍历出的是数组下标
2.可以用于遍历对象的所有属性。 注意:使用for-in遍历对象属性的时候,遍历出来的是属性名
4.2、实例
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>for...in语句练习</title>
</head>
<script type="text/javascript">
//for-in语句遍历数组
var arr=[12,17,13,19,20];
for(var a in arr){
document.write(arr[a]+" ");//返回值:12 17 13 19 20 这里,如果直接输出a,得到的是该数组的下标值
} document.write("<br/>");//换行
//for-in语句遍历对象的属性值
function person(id,name){
this.id=id;
this.name=name;
}
var p=new person(110,"张三");
for(var a in p){
document.write(p[a]+" ");//返回值:110 张三 这里,如果直接输出a,得到的是该对象的属性名
}
</script>
<body>
</body>
</html>
实例结果图
附录1
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script type="text/javascript">
/*
with语句:有了with语句,在存储对象属性和调用方法时候不用重复指定对象了 格式:
with(对象){ }
*/
with(document){
for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
write("* ");//这里本来要写document.write()才能把值打印出到页面上,要想不重复写document,就在with()括号中写上document
}
write("</br>");
}
} function person(id,name){
this.id=id;
this.name=name;
}
document.write("<hr/>");
var p=new person(110,"狗娃");
with(p){
document.write(id+" "+name);//这里本来要p.id和p.name才能获取到值的,要想不重复写p,则在with()括号中写上p即可,然后用大括号括起来
} </script>
<body>
</body>
</html>
附录2
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script type="text/javascript">
//需求1:打印出"*"的正方形,5行5列
for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
document.write("* ");
}
document.write("<br/>");
} document.write("<hr/>");//水平线
//需求2:打印出一个正直角三角形,5行5列
for(var i=0;i<5;i++){
for(var j=0;j<=i;j++){
document.write("* ");
}
document.write("<br/>");
} document.write("<hr/>");//水平线
//需求3:打印出一个倒直角三角形,5行5列
for(var i=0;i<5;i++){
for(var j=4;j>=i;j--){
document.write("* ");
}
document.write("<br/>");
} document.write("<hr/>");//水平线
//需求4:打印出一个九九乘法表
for(var i=1;i<10;i++){
for(var j=1;j<=i;j++){
document.write(j+"*"+i+"="+(i*j)+" ");
}
document.write("<br/>");
}
</script>
<body>
</body>
</html>
附录2结果图
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/9416306.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |
java基础61 JavaScript循环语句之while、do...while、for及for...in循环(网页知识)的更多相关文章
- java基础63 JavaScript中的Number、Math、String、Date对象(网页知识)
本文知识点(目录): 1.Number对象 2.Math对象 3.String对象 4.Date对象 (日历例子) 1.Number对象 1.1.Number对象的创建方式 方式1: ...
- Java基础break、continue语句的用法
break适用范围:只能用于switch或者是循环语句中.当然可以用于增强for循环. break作用: 1. break用于switch语句的作用是结束一个switch语句. 2. break用于循 ...
- Java基础语法(基本语句)
Java基础语法 标识符在程序中自定义的一些名称.由26个英文字母大小写,数字:0-9符号:_&组成定义合法标识符规则:1. 数字不可以开头2. 不可以使用关键字Java中 ...
- java基础之运算符与语句
一.运算符 1.算数运算符 运算符 名称 举例 + 加法 A等于10,B等于3 则A+B=13 - 减法 A等于10,B等于3 则A-B=7 * 乘法 A等于10,B等于3 则A*B=30 / 除法 ...
- java基础58 JavaScript的几种格式和变量的声明方式(网页知识)
1.JavaScript的几种格式 1.1.JavaScript的特点 1.跨平台性 2.安全性.(javaScript代码不能直接访问电脑硬盘上的信息) 1.2.Java与javaScript ...
- C#语句2——循环语句(for穷举、迭代和while循环)
一.for循环拥有两类: (一).穷举: 把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元. ...
- java基础59 JavaScript运算符与控制流程语句(网页知识)
1.JavaScript运算符 1.1.加减乘除法 加法:+(加法,连接符,正数) true是1,false是0 减法:- 乘法:* 除法:/ 1.2.比较运算符 ...
- java基础69 JavaScript产生伪验证码(网页知识)
1.伪验证码 <!doctype html> //软件版本:DW2018版 <html> <head> <meta charset="utf-8&q ...
- java基础64 JavaScript中的Arrays数组对象和prototype原型属性(网页知识)
1.Arrays数组对象的创建方式 方式一: var 变量名=new Array(); //创建一个长度为0的数组. 方式二: var 变量名=new Array(长度); //创建一个指定长度的数组 ...
随机推荐
- 【codeforces 778C】 Peterson Polyglot
http://codeforces.com/problemset/problem/778/C (题目链接) 题意 给出一个字典树,问删掉哪一层以后,得到的字典树最小. Solution 直接对于每一层 ...
- 【uoj129】 NOI2015—寿司晚宴
http://uoj.ac/problem/129 (题目链接) 题意 给出2~n这n-1个数,求选2个集合,使得从两集合中任意各选取1个数出来它们都互质.求方案数. Solution PoPoQQQ ...
- Android O 正式版新功能
ref: Android O新特性和行为变更总结zzhttp://www.cnblogs.com/bluestorm/p/7148134.html Android O正式版带来了诸多新功能,如Tens ...
- D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)
http://codeforces.com/contest/851/problem/D 分区间操作 #include <cstdio> #include <cstdlib> # ...
- C#线程篇---解答线程之惑(2)
我们都知道,在这个行业,追求的就是用最少的时间学最多的知识,这是我写这个系列最想达到的目标,在最快的时间内,帮助更多的人学习更多的线程知识. 前一篇,讲述了线程基础,给大家铺垫了一个基础,这一篇着重介 ...
- [leetcode]multiply-strings java代码
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚
P4644 [Usaco2005 Dec]Cleaning Shifts 清理牛棚 你有一段区间需要被覆盖(长度 <= 86,399) 现有 \(n \leq 10000\) 段小线段, 每段可 ...
- bzoj千题计划135:bzoj1066: [SCOI2007]蜥蜴
http://www.lydsy.com/JudgeOnline/problem.php?id=1066 每个柱子拆成两个点 i<<1,i<<1|1,之间连流量为高度的边 如果 ...
- python学习笔记3-函数的递归
递归就是指自己函数的自我调用 #递归 #自己调用自己,函数的循环 def test1(): num = int(input('please enter a number:')) if num%2==0 ...
- python 12306 车次数据获取
ssl._create_default_https_context = ssl._create_default_https_context train_data = '2018-10-20' head ...