1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-23 22:52:08 +01:00
Files
archived-doc-zh/language/control-structures/do-while.xml
2026-02-10 11:36:45 +08:00

88 lines
2.6 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 53fb200fed6d316b0616a915eb87a40de1d80f51 Maintainer: dallas Status: ready -->
<sect1 xml:id="control-structures.do.while" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>do-while</title>
<?phpdoc print-version-for="dowhile"?>
<simpara>
<literal>do-while</literal> 循环和 <literal>while</literal>
循环非常相似,区别在于表达式的值是在每次循环结束时检查而不是开始时。和一般的
<literal>while</literal> 循环主要的区别是 <literal>do-while</literal>
的循环语句保证会执行一次(表达式的真值在每次循环结束后检查),然而在一般的
<literal>while</literal> 循环中就不一定了(表达式真值在循环开始时检查,如果一开始就为
&false; 则整个循环立即终止)。
</simpara>
<para>
<literal>do-while</literal> 循环只有一种语法:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
以上循环将正好运行一次,因为经过第一次循环后,当检查表达式的真值时,其值为
&false;<varname>$i</varname> 不大于 0而导致循环终止。
</simpara>
<para>
资深的 C 语言用户可能熟悉另一种不同的 <literal>do-while</literal>
循环用法,把语句放在 <literal>do-while</literal>(0) 之中,在循环内部用
<link linkend="control-structures.break"><literal>break</literal></link>
语句来结束执行循环。以下代码片段示范了此方法:
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
do {
if ($i < 5) {
echo "i is not big enough";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "i is ok";
/* 处理 i */
} while(0);
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
可以使用 <link linkend="control-structures.goto"><literal>goto</literal></link> 跳出循环,取代这种 hack 的方式。
</simpara>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->