do と while を比較する課題アクティビティの解答を確認する
コード ブロックを少なくとも 1 回実行する必要があることがわかっているため、以下の例では do
を使う必要があります。 while
を使って同じ結果を得ることもできます。 while
のロジックを使うとコードがいっそう読みやすくなると感じる開発者もいます。 その場合は、while
の実装を選んでもかまいません。 この場合、ほとんどのコード コンパイラでは、反復ステートメントを do-while
に変換することで、コードが自動的に最適化されることに注意してください。
プロジェクト 1 のコード
次のコードは、前のユニットの課題プロジェクト 1 に対して考えられる解答の 1 つです。
コード ブロックを少なくとも 1 回は実行する必要があるため、このコードでは do
ステートメントを使います。 while
を使って同じ結果を得ることもできます。 while
のロジックを使うとコードがいっそう読みやすくなると感じる開発者もいます。 その場合は、ここで while
ステートメントの実装を選んでもかまいません。
string? readResult;
string valueEntered = "";
int numValue = 0;
bool validNumber = false;
Console.WriteLine("Enter an integer value between 5 and 10");
do
{
readResult = Console.ReadLine();
if (readResult != null)
{
valueEntered = readResult;
}
validNumber = int.TryParse(valueEntered, out numValue);
if (validNumber == true)
{
if (numValue <= 5 || numValue >= 10)
{
validNumber = false;
Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");
}
}
else
{
Console.WriteLine("Sorry, you entered an invalid number, please try again");
}
} while (validNumber == false);
Console.WriteLine($"Your input value ({numValue}) has been accepted.");
readResult = Console.ReadLine();
プロジェクト 2 のコード
次のコードは、前のユニットの課題プロジェクト 2 に対して考えられる解答の 1 つです。
コード ブロックを少なくとも 1 回は実行する必要があるため、このコードでは do
ステートメントを使います。 while
を使って同じ結果を得ることもできます。 while
のロジックを使うとコードがいっそう読みやすくなると感じる開発者もいます。 その場合は、ここで while
ステートメントの実装を選んでもかまいません。
string? readResult;
string roleName = "";
bool validEntry = false;
do
{
Console.WriteLine("Enter your role name (Administrator, Manager, or User)");
readResult = Console.ReadLine();
if (readResult != null)
{
roleName = readResult.Trim();
}
if (roleName.ToLower() == "administrator" || roleName.ToLower() == "manager" || roleName.ToLower() == "user")
{
validEntry = true;
}
else
{
Console.Write($"The role name that you entered, \"{roleName}\" is not valid. ");
}
} while (validEntry == false);
Console.WriteLine($"Your input value ({roleName}) has been accepted.");
readResult = Console.ReadLine();
プロジェクト 3 のコード
次のコードは、前のユニットの課題プロジェクト 3 に対して考えられる解答の 1 つです。
"foreach 反復変数" に割り当てられた値を変更することはできないため、このコードでは外側のループに for
ステートメントを使っています。 foreach
ループの内側で追加の文字列変数を宣言することでこれを回避できますが、コード ロジックに不要な複雑さが加わることになります。 つまり、反復ステートメント foreach (string myString in myStrings)
を使ってから、myString
変数を処理しようとすると、コンパイル エラーが発生します。
データ文字列の値によっては (文字列にピリオドが含まれていない場合) コード ブロックが実行されない可能性があるため、このコードでは内側のループに while
ステートメントを使っています。 反復ブロックを実行する必要がない可能性がある場合は、do
ステートメントを使わないでください。
string[] myStrings = new string[2] { "I like pizza. I like roast chicken. I like salad", "I like all three of the menu choices" };
int stringsCount = myStrings.Length;
string myString = "";
int periodLocation = 0;
for (int i = 0; i < stringsCount; i++)
{
myString = myStrings[i];
periodLocation = myString.IndexOf(".");
string mySentence;
// extract sentences from each string and display them one at a time
while (periodLocation != -1)
{
// first sentence is the string value to the left of the period location
mySentence = myString.Remove(periodLocation);
// the remainder of myString is the string value to the right of the location
myString = myString.Substring(periodLocation + 1);
// remove any leading white-space from myString
myString = myString.TrimStart();
// update the comma location and increment the counter
periodLocation = myString.IndexOf(".");
Console.WriteLine(mySentence);
}
mySentence = myString.Trim();
Console.WriteLine(mySentence);
}