While statements- forgive the noob
I am new to C# and created the following while loop:
while (months <= 12)
while (months >= 60)
{
Console.Write("Invalid months. Must be >= 12 and <= 60");
Console.Write("\nEnter the number of months in the lease::");
months = Convert.ToInt32(Console.ReadLine());
}
Is this correct?
Re: While statements- forgive the noob
You need to do a Console.ReadLine before entering the loop (months will be 0 unless you do). And then again inside the loop (like you already are).
If you don't want to do the Console.ReadLine prior to the while, you can set it to an arbitrary number to get in the loop, then move the Console.ReadLine to the beginning of the loop e.g.
Code:
Int32 months = 12;
while (months >= 12 && months <= 60)
{
months = Convert.ToInt32(Console.ReadLine());
if (months >= 12 && months <= 60) { break; }
Console.Write("Invalid months. Must be >= 12 and <= 60");
Console.Write("\nEnter the number of months in the lease::");
}
You also need an exit from the loop if the entered value is in the appropriate range. This is what the if statement does.