Earn More by Sharing What You Love
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!

Layla Porter
7 分 37 秒
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?")
Dim doTheyLikeCheese As String = Console.ReadLine()そして、この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" Then
Console.WriteLine("Here is a lump of cheese.")
Else
Console.WriteLine("Here is a lump of coal.")
End Ifユーザーが"はい"と答えた場合、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" Then
Console.WriteLine("Here is a lump of cheese.")
Else
Console.WriteLine("Here is a lump of coal.")
End IfLaylaは次に、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" Then
Console.WriteLine("Here is a lump of cheese.")
ElseIf doTheyLikeCheese.ToLower() = "no" Then
Console.WriteLine("Here is a lump of coal.")
Else
Console.WriteLine("Please give a yes or no answer.")
End If前と同じように、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
Dim hasAnswered As Boolean = False
While Not hasAnswered
' Checking the user's response and setting hasAnswered to true for valid responses
If doTheyLikeCheese.ToLower() = "yes" Then
Console.WriteLine("Here is a lump of cheese.")
hasAnswered = True
ElseIf doTheyLikeCheese.ToLower() = "no" Then
Console.WriteLine("Here is a lump of coal.")
hasAnswered = True
Else
Console.WriteLine("Please give a yes or no answer.")
doTheyLikeCheese = Console.ReadLine()
End If
End While上記の例では、ユーザーが"はい"または"いいえ"と答えるまで、ループがプロンプトを表示し続けます。 Laylaの動画から、コードが実行される様子をご覧ください:

要約すると
条件ロジックはC#プログラミング言語の基本であるため、これらの概念を練習し、コードに含めることを開始するのが最善です。 そして、これはほんの始まりに過ぎません。 if文、else文、else-ifラダー、if-else内のif-else、入れ子のif-else、switch文など、条件ロジックはプログラムを劇的に変化させることができ、学ぶべきことがたくさんあります。
そして、完成です! 以上、if-else文とwhileループの初心者向けガイドでした。 前述したように、このガイドは LaylaCodesIt にインスパイアされたものです。彼女のチャンネルで、上記のすべてのデモンストレーション動画や、C# プログラミングに関する詳細情報をご覧ください。
C#のスキルをレベルアップするために、お気に入りのクリエイター、チュートリアル、学習リソースを紹介しています。 コーディングを楽しんでください!
Do you create content for developers working with .NET, C#, Java, Python, or Node.js? Turn your expertise into extra income!
Join our newsletter, you’ll get exclusive access on article updates. We value your privacy