While文
[Wikipedia|▼Menu]

while文 (: while statement) はプログラミング言語において繰り返し(ループ)の制御構造を記述するための (statement) である。英語の接続詞 while の意味「〜である間」の通り、継続条件として指定された(制御式)を評価した値が真である間、ループ本体 (loop body) [1]の処理を繰り返し実行する。

while文では通例、ループの最初に継続条件式を評価する。したがって、継続条件式が偽であった場合はループ本体の処理は一度も実行されない。プログラミング言語によっては、ループの本体を一度実行した後、継続条件式を評価するdo-while文をサポートするものもある。do-while文では継続条件式の真偽にかかわらず、ループ本体の処理が必ず一度は実行される。詳細は当該記事も参照のこと。

なお、関数型言語では通例、whileループは文ではなく式 (expression) として定義される。while式はwhile文よりもプログラム中で記述できる位置に関して柔軟性がある。Haskellのような純粋関数型言語では、ループは常に再帰末尾再帰)を使って記述するため、whileループの構文をサポートしないものもある。

Cおよびそれに類する言語

C言語, C++, C#, D, Java, Perlなどでは以下のような構文である。while (条件) 文 // ここの部分を「ループ本体」と呼ぶ

このループの実行は、次のような手順となる。
「条件」を評価する。「条件」がならば、ループを終了する。

「文」を実行する。

「条件」の評価に戻る。

「条件」がはじめから偽の場合は、「文」は一度も実行されない。

ループ本体が複数の文からなる場合、ブロック(複文)を使う。while (条件) { ...}
プログラム例int x = 0;while (x < 100) { printf("x は %d です。\n", x); ++x; // x の値を 1 だけ増やす(インクリメントする)。}

これを実行すると、次のように標準出力に出力する。x は 0 です。x は 1 です。………… x は 98 です。x は 99 です。

変数xの初期値は0であり、ループ本体の処理を1回実行するたびにインクリメントされる。xの値が100未満すなわち99のときまではループ本体の処理が実行され、最終的にxの値が100になる。その後、継続条件式を評価するとき、条件は成り立たなくなり、ループは終了する。
ループの脱出と継続

ループの脱出と継続の制御に利用できる分岐文を備える言語もある。break文は、ループ本体の複文の途中からであっても、またwhileの条件が成り立っていても、ループ中から抜け出す。continue文はループの途中から、ループ本体の最後に飛び[2]、while文の場合にはそこから先頭に戻って条件の評価となる。

goto文を使える言語では、whileループの脱出にgoto文を使うこともできる。通常はbreak文のほうが好ましいが、多重ループを脱出する場合やswitch文をループ本体に含む場合などでは、goto文のほうが簡潔に記述できる。return文や例外のスローによってループを脱出することのできる言語もある。
Pascal

Pascalにおけるwhile文は、C言語系列と概ね同様であり、キーワードとして while と do を使う。一方、do-while文に関しては、repeat-until文が相当するが、キーワードとして repeat と until を使う点が異なるだけでなく、制御式として継続条件ではなく終了条件(真のときループを終了)を記述する点も異なる。
構文while 条件 do 文repeat 文; 文...; 文 until 条件

LL(1)の単純な構文というPascalのポリシーから、両者で全く違うキーワードを使う設計(デザイン)となっている。

whileでは複数の文を置く場合には begin-end で複文にしなければならないが、repeat-until はそれ自身が暗黙のブロック構文になっているので、セミコロンで区切って複数の文を置くことができる。
Basic

Full BASIC(INCITS/ISO/IEC 10279-1991 (R2005) "Information Technology ? Programming Languages ? Full BASIC")には、ループの多くのパターンに対応する柔軟性のあるDo-Loop文がある。またVisual Basicには、それに加えて伝統的なBASICのWHILEに近いWhile文もある。
構文

Visual Basic .NETにおけるWhile文は以下のような構文である[3]Visual Basicでは While...Wend だが、VB.NETでは While...End While となっている。While 条件 文... [ (Continue|Exit) While ] 文...End While

Exit While はbreak文、Continue While はcontinue文に相当する機能を持つ。

Do...Loop文は以下のような構文である[4]。Do [(While|Until) 条件] 文... [ (Continue|Exit) Do ] 文...Loop [(While|Until) 条件]

Do の後に While を続ければ、while文に相当し、Loop の後に Until を続ければ、Pascalの repeat-until に相当する。どちらにも条件を付けなければ、単純な無限ループになる。
^ JIS X 3010:2003「プログラム言語C」§6.8.5「繰返し文」
^ JIS X 3010:2003「プログラム言語C」§6.8.6.2「continue文」
^ While...End While Statement (Visual Basic) 。Microsoft Docs
^ Do...Loop Statement (Visual Basic) 。Microsoft Docs

関連項目

if文

for文

do-while文

goto文

制御構造

無限ループ
.mw-parser-output .asbox{position:relative;overflow:hidden}.mw-parser-output .asbox table{background:transparent}.mw-parser-output .asbox p{margin:0}.mw-parser-output .asbox p+p{margin-top:0.25em}.mw-parser-output .asbox{font-size:90%}.mw-parser-output .asbox-note{font-size:90%}.mw-parser-output .asbox .navbar{position:absolute;top:-0.90em;right:1em;display:none}

この項目は、コンピュータに関連した書きかけの項目です。この項目を加筆・訂正などしてくださる協力者を求めていますPJ:コンピュータ/P:コンピュータ)。
.mw-parser-output .hlist ul,.mw-parser-output .hlist ol{padding-left:0}.mw-parser-output .hlist li,.mw-parser-output .hlist dd,.mw-parser-output .hlist dt{margin-right:0;display:inline-block;white-space:nowrap}.mw-parser-output .hlist dt:after,.mw-parser-output .hlist dd:after,.mw-parser-output .hlist li:after{white-space:normal}.mw-parser-output .hlist li:after,.mw-parser-output .hlist dd:after{content:" ・\a0 ";font-weight:bold}.mw-parser-output .hlist dt:after{content:": "}.mw-parser-output .hlist-pipe dd:after,.mw-parser-output .hlist-pipe li:after{content:" |\a0 ";font-weight:normal}.mw-parser-output .hlist-hyphen dd:after,.mw-parser-output .hlist-hyphen li:after{content:" -\a0 ";font-weight:normal}.mw-parser-output .hlist-comma dd:after,.mw-parser-output .hlist-comma li:after{content:"、";font-weight:normal}.mw-parser-output .hlist-slash dd:after,.mw-parser-output .hlist-slash li:after{content:" /\a0 ";font-weight:normal}.mw-parser-output .hlist dd:last-child:after,.mw-parser-output .hlist dt:last-child:after,.mw-parser-output .hlist li:last-child:after{content:none}.mw-parser-output .hlist dd dd:first-child:before,.mw-parser-output .hlist dd dt:first-child:before,.mw-parser-output .hlist dd li:first-child:before,.mw-parser-output .hlist dt dd:first-child:before,.mw-parser-output .hlist dt dt:first-child:before,.mw-parser-output .hlist dt li:first-child:before,.mw-parser-output .hlist li dd:first-child:before,.mw-parser-output .hlist li dt:first-child:before,.mw-parser-output .hlist li li:first-child:before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child:after,.mw-parser-output .hlist dd dt:last-child:after,.mw-parser-output .hlist dd li:last-child:after,.mw-parser-output .hlist dt dd:last-child:after,.mw-parser-output .hlist dt dt:last-child:after,.mw-parser-output .hlist dt li:last-child:after,.mw-parser-output .hlist li dd:last-child:after,.mw-parser-output .hlist li dt:last-child:after,.mw-parser-output .hlist li li:last-child:after{content:")\a0 ";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li:before{content:" "counter(listitem)" ";white-space:nowrap}.mw-parser-output .hlist dd ol>li:first-child:before,.mw-parser-output .hlist dt ol>li:first-child:before,.mw-parser-output .hlist li ol>li:first-child:before{content:" ("counter(listitem)" "}.mw-parser-output .navbar{display:inline;font-size:75%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}.mw-parser-output .infobox .navbar{font-size:88%}.mw-parser-output .navbox .navbar{display:block;font-size:88%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}


次ページ
記事の検索
おまかせリスト
▼オプションを表示
ブックマーク登録
mixiチェック!
Twitterに投稿
オプション/リンク一覧
話題のニュース
列車運行情報
暇つぶしWikipedia

Size:12 KB
出典: フリー百科事典『ウィキペディア(Wikipedia)
担当:undef