Home  >  Article  >  Backend Development  >  Detailed explanation of while loop usage

Detailed explanation of while loop usage

hzc
hzcOriginal
2020-06-17 16:27:218654browse

Detailed explanation of while loop usageDetailed explanation of while loop usage:

While is a basic loop mode of computers. When the condition is met, it enters the loop. After entering the loop, when the condition is not met, it exits the loop. The general expression of the while statement is: while (expression) {loop body}.

Typical loop

WHILE <条件>
<语句体>
end while
do while <条件>
<语句体>
loop

Grammar

Pascal
while <条件> do <语句>
意为当条件符合时,接着做下面的语句;不符合时,退出循环。
Python
whileexpression:
...
else:
...
当满足条件expression时运行,不满足时执行else下方语句。
C
do <语句> while(<条件>);
while(<条件>) <语句>;
C++
while(<条件>) <语句>;
do <语句> while(<条件>);
Java
while(<条件>) {<语句;>}
do {<语句;>} while(<条件>);
二者的区别是do-while最少会被执行一次。
循环中可以使用continue结束当前循环,回到循环开始处开始下一次循环。也可以用break跳出整个循环。
javascript
JavaScript中while循环的目的是为了反复执行语句或代码块。
只要指定条件为true,循环就可以一直执行代码块。 [1] 
JavaScript中while循环的语法如下:
while (<条件>) {需执行的代码 };
do {需执行的代码 } while (<条件>);
注意:do...while 循环是 while 循环的变种。该循环程序在初次运行时会首先执行一遍其中的代码,然后当指定的条件为 true 时,它会继续这个循环。所以可以这么说,do...while 循环为执行至少一遍其中的代码,即使条件为 false,因为其中的代码执行后才会进行条件验证。
PHP
while 循环是 php 中最简单的循环类型。它和 C 语言中的 while 表现得一样。语法如下:
while(expr){
statement
}

The above is the detailed content of Detailed explanation of while loop usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does NOCC mean?Next article:What does NOCC mean?