while文 2015/04/05
特定の条件を満たしている間同じ処理を繰り返す構文の、プログラミング言語別のメモ。
配列やリストの要素に対して繰り返し処理をさせる方法は foreachも参照。
C言語, C++ 2013/07/14
while (...) {
...
}
本体の文が1つだけの場合は {} を省略可。
while (...)
...
永久ループは
while (true) {
...
}
または
for (;;) {
...
}
Go言語 2015/09/26
for ... {
...
}
本体の文が1つだけの場合でも {} を省略不可。
永久ループは
for {
...
}
For statements | The Go Programming Language Specification
http://golang-jp.org/ref/spec#For_statements
Java 2014/08/08
while (...) {
...;
}
本体が式1つだけの場合は {} を省略可
while (...)
...;
永久ループは
while (true) {
...;
}
条件式の部分は他の多くの言語と違ってboolean型でないとコンパイルエラーが発生する。
C言語と同様にdo while文もある。
The while Statement | Java Language Specification
http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.12
The do Statement | Java Language Specification
http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.13
Scala 2014/10/03
while文は関数型言語っぽくないけど、いちおうある。
while (...) {
...;
}
本体が式1つだけの場合は {} を省略可
while (...)
...;
Scalaのif式などと違い、while は常に Unit を返す。
break や continue は存在しない。再帰関数を使うなどする。
永久ループは
while (true) {
...;
}
PHP 2014/04/27
while (...) {
...;
}
コロンを使った以下の記法も可能。
while (...):
...;
endwhile;
C言語と同様にdo while文もある。
while文 | PHP Manual
http://www.php.net/manual/ja/control-structures.while.php
do while文 | PHP Manual
http://www.php.net/manual/ja/control-structures.do.while.php
Python 2014/07/31
while ...:
...
...
for文と同じく、
else節を付けることができる。else節は条件を満たさなくなってループを脱出する際に実行される。ただし、breakで脱出した場合は実行されない。このelse節は他の言語には見られない機能だ。
while ...:
...
...
else:
...
...
永久ループは
while True:
...
...
for文で書けるものはwhile文でも書けるが、できるだけfor文で書いたほうが短く書け、わかりやすいコードになる。実行速度もfor文のほうが速いらしい。
while文 | Python 2.7 documentation
http://docs.python.jp/2/reference/compound_stmts.html#while
while文 | Python 3 documentation
http://docs.python.jp/3/reference/compound_stmts.html#the-while-statement
Ruby 2013/12/19
while ...
...
end
以下のように条件の後に do を付けてもよい。
while ... do
...
end
条件式を否定形に反転した until もある。
until ...
...
end
until も条件の後ろに do を付けてもよい。
永久ループは以下のようにする。
while true
...
end
Rubyでは while, until は結果を返すことができるが通常は nil を返す。引数を伴った break で特定の値を while, until が返すことができる。
Perl 2013/09/06
while (...) {
...;
}
条件が否定になった以下の形式もある。
until (...) {
...;
}
doループも使える。
do {
...;
} while (...);
do {
...;
} until (...);
修飾子の書き方も可能。
... while ...;
... until ...;
永久ループを実現するには単に条件を省略する。
while () {
...
}
sh (シェルスクリプト) 2014/05/28
条件式を書けるtestコマンド([)での例
while [ ... ]; do
...
done
その他のコマンドでの例
while ...; do
...
done
永久ループの例
while :; do
date;
sleep 1;
done
自分の環境ではループの中身を空にすると Syntax error になった。
ループの中では break が使える。
R言語 2013/11/20
while (...) ...
または
while (...) {
...
}
CoffeeScript 2013/06/24
while ...
...
または1行にまとめて
... while ...
ループの中では break や continue が使える。