zoukankan      html  css  js  c++  java
  • JavaScript While 循环

    While 循环
    利用 while 循环在指定条件为 true 时来循环执行代码。

    while 循环用于在指定条件为 true 时循环执行代码。

    语法:

    while (变量<=结束值)
    {
        需执行的代码
    }
    

    注意:除了<=,还可以使用其他的比较运算符。

    例子:

    var i=0
    while (i<=10)
    {
    document.write("The number is " + i)
    document.write("<br />")
    i=i+1
    }
    
    The number is 0
    The number is 1
    The number is 2
    The number is 3
    The number is 4
    The number is 5
    The number is 6
    The number is 7
    The number is 8
    The number is 9
    The number is 10
    
     
    

    Do while 循环
    利用 do...while 循环在指定条件为 true 时来循环执行代码。在即使条件为 false 时,这种循环也会至少执行一次。这是因为在条件被验证前,这个语句就会执行。

    do...while 循环

    do...while 循环是 while 循环的变种。该循环程序在初次运行时会首先执行一遍其中的代码,然后当指定的条件为 true 时,它会继续这个循环。所以可以这么说,do...while 循环为执行至少一遍其中的代码,即使条件为 false,因为其中的代码执行后才会进行条件验证。

    语法:

    do
    {
        需执行的代码
    }
    while (变量<=结束值)
    
    例子:
    
    var i=0
    do 
    {
    document.write("The number is " + i)
    document.write("<br />")
    i=i+1
    }
    while (i<0)
    
    The number is 0
    
    
    
  • 相关阅读:
    bzoj 4548 小奇的糖果
    CF1151F Sonya and Informatics
    loj 2392「JOISC 2017 Day 1」烟花棒
    loj 2336「JOI 2017 Final」绳
    luogu P3620 [APIO/CTSC 2007]数据备份
    bzoj 4771 七彩树
    CF765F Souvenirs
    bzoj 3145 [Feyat cup 1.5]Str
    luogu P4482 [BJWC2018]Border 的四种求法
    第五章例题
  • 原文地址:https://www.cnblogs.com/backuper/p/1370858.html
Copyright © 2011-2022 走看看