0

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 myclass = 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");
        }
    }
}
6
  • Please describe the error you get. Commented Sep 15, 2010 at 3:21
  • @randy, editing the code in the question is not really helpful (unless you are providing better context). Your edits are changing the nature of the problem entirely, which makes the answers you have received ineffective for other users that have similar problems to the one you original encountered. Fix your code in Visual Studio, let your original code stand so that future users can find solutions. If you run into additional errors, post seperate questions for those. Commented Sep 15, 2010 at 3:56
  • @randy, when you declare userin, give it an initial value. string userin = null; will work just fine. This new error is about using a variable that you have not initialized (because you compare it in the while portion without having assigned it an original value). And please, stop changing your question! Post a new one! Commented Sep 15, 2010 at 4:07
  • now I get a new error Error 1 A local variable named 'userin' is already defined in this scope Commented Sep 15, 2010 at 4:09
  • for something so simple seems so stupidly difficult Commented Sep 15, 2010 at 4:10

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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
}

Comments

0

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.

2 Comments

I thought I would use the while loop while(userin!="x","X")
@randy That isn't valid syntax to my knowledge. When performing multiple comparisons, you need to specify both sides of the comparison every time. If you understand collections, you could make a List of Strings and use .contains(), but that's overly complicated for this scenario.
0

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...

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.