JavaScript Day_03

JavaScript Day_03

1.循环语句

一.循环的概念

循环就是重复做一件事,在程序中指的是重复执行某段代码

二。死循环

死循环是指重复执行代码,不会停止,死循环会造成程序卡死甚至崩溃等问题

三.循环的用处

循环的用处广泛,特别是对大量数据进行操作的时候

四.循环的分类

循环有很多种方式,如:while,do-while,for,for-in,for-each

while循环
while(表达式){
    执行代码
}
{}所包含的称为循环体
do-while循环
do{
    执行代码
}while(表达式)
do-whilewhile的区别在于while是先判断条件,再执行代码,do-while是先执行,再判断,所以不管do-while表达式的条件是否成立,都至少会执行一次循环

while循环和do-while循环的时间复杂度一致,while循环的循环速度比for循环快一倍,while的时间复杂度比for的低

for循环
for(表达式1;表达式2;表达式3){
    执行代码
}
表达式1:可写循环初始值,也可不填
表达式2:循环条件
表达式3:循环增量(减量)
两个分号必须写
break关键字

功能:

1.在switch语句中使流程跳出switch结构

2.在循环语句中使流程跳出当前循环

注意:

1.如果已执行break语句,就不会执行循环体中位于break之后的语句

2.在多层循环中,一个break只向外跳一层

continue关键字

功能:只能在循环语句中使用,是本次循环结束,即跳过循环体中下面还未执行的语句,接着进行下次是否执行循环的判断

注意:

1.continue语句只能用在循环中

2.对于while和do-while,continue语句执行之后的动作是条件判断,即是否还能继续进行while或do-while循环;对于for循环,随后的动作是变量更新,即i++;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你想了解 JavaScript 实现的日历吗? JavaScript 可以通过 DOM 操作实现动态生成日历的功能。以下是一个简单的 JavaScript 日历实现示例: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript 日历示例</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: center; padding: 8px; border: 1px solid black; } th { background-color: #ddd; } td { height: 80px; } .today { background-color: yellow; } </style> </head> <body> <h1>JavaScript 日历示例</h1> <table> <thead> <tr> <th>日</th> <th>一</th> <th>二</th> <th>三</th> <th>四</th> <th>五</th> <th>六</th> </tr> </thead> <tbody id="calendar-body"> </tbody> </table> <script> const date = new Date(); const year = date.getFullYear(); const month = date.getMonth() + 1; const today = date.getDate(); const daysInMonth = new Date(year, month, 0).getDate(); const firstDay = new Date(year, month - 1, 1).getDay(); const calendarBody = document.getElementById('calendar-body'); let dateCells = ''; let day = 1; for (let i = 0; i < 6; i++) { dateCells += '<tr>'; for (let j = 0; j < 7; j++) { if (i === 0 && j < firstDay) { dateCells += '<td></td>'; } else if (day > daysInMonth) { break; } else { if (day === today) { dateCells += '<td class="today">' + day + '</td>'; } else { dateCells += '<td>' + day + '</td>'; } day++; } } dateCells += '</tr>'; if (day > daysInMonth) { break; } } calendarBody.innerHTML = dateCells; </script> </body> </html> ``` 这个示例会生成当前月份的日历,可以根据需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值