📜  JavaScript While 循环

📅  最后修改于: 2022-05-13 01:56:34.065000             🧑  作者: Mango

JavaScript While 循环

Javascript 中的While 循环是一种控制流语句,允许根据给定的布尔条件重复执行代码。 while 循环可以被认为是一个重复的 if 语句。

循环可用于多次执行特定代码块,直到它未能匹配条件。

主要有两种类型的循环:

  • 入口控制循环:在这种类型的循环中,测试条件在进入循环体之前被测试。 For 循环While 循环是入口控制循环。
  • 退出受控循环:在这种类型的循环中,测试条件在循环体的末尾进行测试或评估。因此,无论测试条件是真还是假,循环体都会至少执行一次。 do-while 循环是退出控制循环。

句法:

while (condition) {
    // Statements
}

示例:此示例说明了 while 循环的使用。

HTML


 

    JavaScript While loop

 

    
        

GeeksForGeeks

        

JavaScript While Loop

    
    

                   


HTML


 

    JavaScript While loop

 

    
        

GeeksforGeeks

        

JavaScript Do-while Loop

    

                   


HTML


 

    JavaScript loop

 

    
        

            GeeksforGeeks         

        

JavaScript Loop

    

While Loop

    

                      

Do While Loop

    

             


输出:

while 循环

Do-While 循环: do-while 循环是一个控制流语句,它至少执行一个代码块一次,然后根据块末尾的给定布尔条件重复执行该块或不执行该块。

句法:

do {
    // Statements
}
while (condition);

示例:此示例说明了 do-while 循环的使用。

HTML



 

    JavaScript While loop

 

    
        

GeeksforGeeks

        

JavaScript Do-while Loop

    

                   

输出:

Do-while 循环

while 和 do-while 循环的比较: do-while 循环在检查 while 循环的条件之前执行一次循环的内容。 while 循环会在执行内容之前先检查条件。

While Loop

Do-While Loop

It is an entry condition looping structure.

It is an exit condition looping structure.

The number of iterations depends on the condition mentioned in the while block.

Irrespective of the condition mentioned in the do-while block, there will a minimum of 1 iteration.

The block control condition is available at the starting point of the loop.

The block control condition is available at the endpoint of the loop.

示例:此示例说明了 while 和 do-while 循环。

HTML



 

    JavaScript loop

 

    
        

            GeeksforGeeks         

        

JavaScript Loop

    

While Loop

    

                      

Do While Loop

    

             

输出:

Javascript循环