1
1

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 1 year has passed since last update.

for文の条件を条件付きで書けるとは知らんかった

Posted at

for文の条件を条件付きで書けるとは知らんかった

書けたら良いのに…とは思ってたけど、試してなかった。
JavaScriptなら書けるのね。
今まで見たことがなかったからメモっとく。

さすがにif文は書けないけど、これで正順でも逆順でも気にしなくて済む。3項演算子は好みじゃないけど、これができるなら使う。

reverse_loop.js
function reverse_loop(from,to)
{
	for(i=from; (from<to?i<=to:i>=to); (from<to?i++:i--)){
		document.writeln(i+'<br />');
	}
	document.writeln('<hr />');
}
reverse_loop(2,5);
reverse_loop(5,2);
1
1
5

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?