2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

条件判定②

Posted at

else文

elseとは「そうでなければ」の意味です。else文ではif文の条件式がfalseだった場合の処理を書く場合に利用する。
イメージ的には以下の様になる。↓


if (条件式1) {
	// 条件式1がtrueならば実行される
} else if (条件式2) {
	// 条件式2がtrueならば実行される
	// 条件式1がtrueのときは実行されない
} else if (条件式3) {
	// 条件式3がtrueならば実行される
	// 条件式2がtrueのときは実行されない
} else {
	// すべての条件式がfalseならば実行される
}
	

てな具合いになる。

具体例だと、以下↓

var x = 30;
if (x > 100) {
    document.write('xは100より大きい値です');
} else if (x > 50) {
    document.write('xは100以下かつ50より大きい値です');
} else {
    document.write('xは50以下の値です');
}; //=> xは50以下の値です

else if_01.png

while文

 while文は以下の様に{}で囲まれたプロック内の文を、()内の条件式がtrueである限り、ブロックの文を繰り返すものである。
イメージ的には以下の様になる↓

while (条件式) {
	文;
	文;
	文;
};

以下例↓

var i = 0;
while (i < 10) {
    document.write(i);
    i++;
}; // 0から9まで出力される

while文1.png

ちなみにi++を取ると、いわゆる有名な「無限ループ」になってブラウザにエラーが出た。
以下そのときの画面。
無限.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?