Error when using while loop
The while loop isnt working. i get an error when it hits the break to get out of the loop. the error is no loop statement to break out of.
static void Main(string[] args)
{
accounts m开发者_运维问答yclass = new accounts();
string userin;
myclass.fillAccounts();
//int i = 0;
//while (i != 1)
do
{
}
while (userin != "x")
{
//use the following menu:
Console.WriteLine("*****************************************");
Console.WriteLine("enter an a or A to search account numbers");
Console.WriteLine("enter a b or B to average the accounts");
Console.WriteLine("enter an x or X to exit program");
Console.WriteLine("*****************************************");
Console.Write("Enter option-->");
userin = Console.ReadLine();
if (userin == "a" || userin == "A")
{
myclass.searchAccounts();
}
else if (userin == "b" || userin == "B")
{
myclass.averageAccounts();
}
else if (userin == "x" || userin == "X")
{
break;
}
else
{
Console.WriteLine("You entered an invalid option");
}
}
}
You have managed to put the body below the actual loop, as far as the code is concerned.
You have:
do
{
} while (criteria);
{
// code you want to execute repeatedly
}
As a result, you get the error message for your break
statement because it is not, in fact, contained within a loop.
You should only have:
while (criteria)
{
// code you want to execute repeatedly
}
Omit to do
portion, as that actually creates a legal loop above the code you really want to loop.
Edit: @Randy, this is the second time you've posted a similar question. You are effectively trying to combine the do
and the while
loops. Go to MSDN and review loops. In short, a while
loop checks the condition before the loop body, the do
loop checks after the loop body. The do
loop also uses the while
keyword, and that may be confusing you.
The Do loop
do
{
// your code...
// this loop is useful when you want the loop to execute at least once, since
// the exit condition will be evaluated *after* the first iteration
} while (condition);
The While loop
while (condition)
{
// your code...
// this loop is useful when you want to prevent *any* iterations unless the
// original boolean condition is met.
}
These are two seperate looping constructs. Combining them does not double your looping fun, it simply creates a single loop (the do
) and then another block of code that follows.
Because it's not in a loop body.
do
{
// This is the do-while loop body.
// You should put the code to be repeated here.
break; // does work
}
while(...)
{
// This is not the do-while loop body.
// This is just an ordinary block of code surrounded by the curly braces
// and therefore declares a local scope. Variables declared in this
// block is not visible outside the braces.
break; // doesn't work
}
Everyone's corrected you on your syntax for the do/while loop, so I won't stress that point.
To allow users to enter their commands case insensitively, you can use if(userIn.ToUpper() == "X")
and not have to check for both conditions.
as in your case in do the first statement is break so it come out of the loop and the statement in while will not be executed...
It was a syntactical mistake you had check this one.
string myString = "X3"; int i = 4; int j = 0; do { if (myString == "X" + j) { break; } j++; } while (j < 4);
精彩评论