Continue文
[Wikipedia|▼Menu]

continue文は、プログラミング言語ループで用いられるである。
文法
Cおよびそれに類する言語の場合

C言語C++JavaJavaScriptなどでは、for文while文do-while文の中で使うことができる。Perlではキーワードが少し異なっており、continueに相当するのはnextで、continueは違う機能のためのものである[1]

continue文は、ループの内部(「ループ本体」の中)にのみ現れることができ、実行されると「それを囲む最も内側の繰返し文のループ継続部,すなわちループ本体の終わりへの分岐を引き起こす。」(JIS X 3010-1993、§6.6.6.2)。

do-while文以外では、その分岐(ジャンプ)先のループ本体の終わりから、そのままループによって先頭に飛び、繰返しのための処理などが行われることになる。do-while文の場合は、ループ本体の終わりにすぐ繰返しの条件判定があるので、ただちに繰返しの条件判定となる。

以上のように、規格票をきちんと読めば、すっきりとした意味があることがわかる。

しかしこれを、おそらくは規格票を読まずに書かれた入門書などによって不自然に理解してしまうと「continue文はその文の実行を打ち切って、そのループの条件式の評価へと制御を移すためのもの」「for文の場合は例外であり、continue文が実行されると条件式の評価の前に、更新式が実行される」「前置判定のwhile文」「後置判定の場合も、同じように動作する。後置判定の場合は、他の方式のループの場合とは異なり、条件式が最後尾に書かれている。しかし冒頭で説明した通り、あくまで条件式の評価へと制御が移る。従って、先頭に戻すのではなく、後の処理を飛ばしてwhile文の最後尾へ制御を移すこととなるため、注意が必要である。」などと、よくわからない説明を重ねる結果になる。

C言語 (C99) による例を示す。制御の流れを視覚化するため、for文の条件式および更新式の中でコンマ演算子を使ってprintf関数の評価結果を無視している。for (int i = 0; printf("condition[%d]\n", i), i < 5; printf("update[%d]\n", i), ++i) { if (i % 2 == 0) { printf("%d is even.\n", i); continue; // ループ本体中の以降の処理は実行されない。 } printf("%d is odd.\n", i);}

結果:condition[0]0 is even.update[0]condition[1]1 is odd.update[1]condition[2]2 is even.update[2]condition[3]3 is odd.update[3]condition[4]4 is even.update[4]condition[5]
脚注^ Perlでは、continueにブロックが引き続いている場合「条件文が再評価される直前に常に実行される」コードとなり、すなわち「C における for ループの 3 番目の部分と同様」な働きとなる。" ⇒http://perldoc.jp/func/continue"

関連項目

for文

while文
.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:8648 Bytes
出典: フリー百科事典『ウィキペディア(Wikipedia)
担当:undef