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
    
    
    
  • 相关阅读:
    187A Permutations
    DFS 专题 哈密顿绕行世界问题
    DFS 专题 N皇后
    DFS专题 Prime Ring Problem
    USACO section1.1 Broken Necklace
    USACO section1.2 Dual Palindromes
    PHPUnitWriting Tests for PHPUnit
    PHP 定界符 使用技巧
    concat函数
    mysql_free_result
  • 原文地址:https://www.cnblogs.com/backuper/p/1370858.html
Copyright © 2011-2022 走看看