C#のIf-Else文を理解する:初心者ガイド
C#プログラミングでは、条件に基づく意思決定は、動的で応答性の高いアプリケーションを作成するための基本的な側面です。 そこで、if-else文が活躍します。 C#のif-else文は、指定された条件が真か偽かによって、プログラムが異なるコードブロックを実行することを可能にします。
このガイドは、LaylaCodesIt によるビデオ"C# for Beginners Part 2 - 'if-else' ステートメントと 'while' ループの書き方を学ぶ"にインスパイアされたもので、C# の if-else ステートメントの基本、ブーリアン条件を記述する際の条件演算子と論理演算子の使用方法を紹介し、プログラムをユーザー入力とインテリジェントに相互作用させる方法を実演します。
条件ロジックの紹介
条件ロジックは、プログラムが特定の条件に基づいて意思決定を行うことを可能にします。 Laylaのビデオでは、条件論理の一例であるif-else文の概念と構文を紹介しています。if-else文は、指定された条件が真か偽かによって異なるコードブロックを実行します。
次のコード例では、Laylaは、ユーザーがチーズが好きかどうかを尋ねる挨拶メッセージをコード化しています:
// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();Simple If-Else Statement in C
そして、このif-else条件文は、ユーザーの答えをチェックし、ユーザーの発言に応じて出力レスポンスを変更します。 条件は条件演算子を使用して構築され、その結果に基づいてif-elseブロック内のステートメントが実行されます。 以下は、1行の文を使った簡単な例です:
// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}ユーザーが"はい"と答えた場合、if文の中のコードはチーズの塊を提供するメッセージを表示します。 もしユーザーが'yes'以外の回答をした場合、elseブロックは "ここに石炭の塊があります "と出力します。
大文字と小文字の区別の問題
基本的なif-else文では、大文字と小文字が区別されるという一般的な問題が発生する可能性があります。 ユーザーが'yes'の代わりに'Yes'を入力した場合、プログラムはそれを有効な入力として認識しません。 この問題を解決するために、LaylaはToLower()メソッドを使用します。これはユーザーの入力を自動的に小文字に変換し、大文字小文字の区別に関する問題を回避します。
// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}複数条件の処理
Laylaは次に、else-if文を使って2つ以上の可能性のある応答を処理する方法を説明します。 このプログラムでは、複数の条件文(ユーザーが"はい"、"いいえ"、またはそれ以外で応答した場合)をチェックします。
// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}前と同じように、if文のブール式が真の場合、ユーザーが"はい"と言えば、プログラムはチーズの塊を提供します。 しかし、else-if文では、他のどのような回答に対しても石炭の塊を提供するのではなく、ユーザーが"いいえ"と回答した場合にのみそうするようにしました。 ユーザーが'はい'か'いいえ'以外の何かで答える場合、プログラムの他の部分は、ユーザーに'はい'か'いいえ'の答えを提供するように要求します。 上記のコードの結果を実際に見てみましょう。

ループの使用
ユーザーが有効な回答を提供できるように、ループを導入しています。 ループは、ユーザーが正しい答えを出すまで繰り返し入力を求めることができます。 これは"while"ループと呼ばれるもので、次の例はその仕組みを示しています:
// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}上記の例では、ユーザーが"はい"または"いいえ"と答えるまで、ループがプロンプトを表示し続けます。 Laylaの動画から、コードが実行される様子をご覧ください:

if-elseステートメント - まとめ
要約すると
- if-else文は、コード内で決定を下すことができます。
- else-if文で複数の条件を扱うことができます。
- ループにより、ユーザーが正しく応答できるようにする
条件ロジックはC#プログラミング言語の基本であるため、これらの概念を練習し、コードに含めることを開始するのが最善です。 そして、これはほんの始まりに過ぎません。 if文、else文、else-ifラダー、if-else内のif-else、入れ子のif-else、switch文など、条件ロジックはプログラムを劇的に変化させることができ、学ぶべきことがたくさんあります。
結論
そして、完成です! 以上、if-else文とwhileループの初心者向けガイドでした。 前述したように、このガイドは LaylaCodesIt にインスパイアされたものです。彼女のチャンネルで、上記のすべてのデモンストレーション動画や、C# プログラミングに関する詳細情報をご覧ください。
C#のスキルをレベルアップするために、お気に入りのクリエイター、チュートリアル、学習リソースを紹介しています。 コーディングを楽しんでください!




