-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathdo-while.xml
96 lines (91 loc) · 2.97 KB
/
do-while.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 7104ee97ced1768a3231588dfc0bc0d7eb1117ad Maintainer: takagi Status: ready -->
<!-- Credits: mumumu -->
<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>ループは最低1回の実行を保証されていることです。
一方、通常の<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 は十分大きくはありません。";
break;
}
$i *= $factor;
if ($i < $minimum_limit) {
break;
}
echo "iはOKです。";
/* i を処理します */
} while (0);
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
この技のかわりに
<link linkend="control-structures.goto"><literal>goto</literal></link>
演算子を使うこともできます。
</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
-->