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?

【C#入門 第3章】条件分岐と繰り返し構文をマスターしてロジック美人に!

Last updated at Posted at 2025-06-05

こんにちは、 CSharpTimes の一之瀬シィよ💠
今回はC#プログラミングの“肝”とも言える「条件分岐」と「繰り返し(ループ)」について教えてあげるわ。


🔀 条件分岐(if / else)

状況に応じて処理を分けたいときに使うのが if 文!

int score = 85;

if (score >= 90)
{
    Console.WriteLine("最高!💯");
}
else if (score >= 70)
{
    Console.WriteLine("いい感じ!👍");
}
else
{
    Console.WriteLine("もっとがんばろ💪");
}

== は「等しいか?」の比較演算子よ。間違って =(代入)と書かないこと!


🧪 論理演算子(&&, ||, !)

複数条件を組み合わせるときはコレ!

int age = 20;
bool hasTicket = true;

if (age >= 18 && hasTicket)
{
    Console.WriteLine("入場OK!");
}
  • &&:かつ
  • ||:または
  • ! :否定(例:!true false

🔁 繰り返し構文(ループ)

🔸 for文:回数が決まってるときに!

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i + "回目のループよ");
}
  • i = 1:スタート
  • i <= 5:継続条件
  • i++:毎回1増える

🔸 while文:条件を満たす間ずっと!

int count = 3;

while (count > 0)
{
    Console.WriteLine("あと" + count + "回!");
    count--;
}

⚠️ 無限ループに注意!

条件がずっと true のままだと、ループが終わらなくなるわよ💢

// 間違った例(終了条件がない)
while (true)
{
    Console.WriteLine("これは止まらない...");
}

📌 まとめ

  • if 文で条件によって処理を分けられる
  • for 文は決まった回数、 while 文は条件が続く限り繰り返す
  • 条件ミスで無限ループにならないよう注意!

次回は、 「第4章:メソッドの定義と使い方」 よ。
そろそろ“効率化の美学”を学んでちょうだい💢

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