0
0

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 3 years have passed since last update.

【C#】<If文>初心者学習メモ

Posted at

書いた理由

  • 自己学習のメモ用
  • 考え方や使い方のストックのため




if文
            for (int i = 0; i < 15; i++) {
                Console.Write("{0} ", i);
                if (i % 5 == 4) {                   //もしiを5で割って4が余ったら
                    Console.WriteLine("");          //改行します。
                }
            }
結果
0  1  2  3  4 
5  6  7  8  9 
10 11 12 13 14

なぜこの一文なのか

例えば「2の倍数の時に○○させる」などは、よく「%」表記を使って

if (i % 2 == 0)

などを見かけてはいました。
しかし、余りを使う条件式を見たことがなく、
今回見た時になるほど…と思ったため学習メモに残します。

**補足メモ**
int i = 0; とした条件なら、
if (i % 改行させたい列の番号 == 改行させたい列の番号 - 1)
という捉え方もできるなと思いました。 

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?