You have textBox1.Text == "Andrea"
and textBox1.Text == "Brittany"
as your loop conditions, but you don't seem to be changing that value anywhere in the code. Therefore, you have an infinite loop which will result in your program crashing.
I'm not certain what your program is meant to be doing, but your options to break out of a loop are:
- Use a
break;
statement to exit the loop.
- Change your loop condition to something which can eventually result in
false
.
- Change the
textBox.Text
property somewhere in the loop body.
Alternatively, you could use an if
statement to check the condition once, and execute some code if that condition is true.
Edit:
I have done this with if statements but now i am looking to try doing the same thing with loops
no purpose just trying to learn how to program
In response to the above comments, I'll tell you how to replace an if statement with a loop. Just do it like so:
// Check the condition before executing the code.
while (textBox1.Text == "Andrea") {
// Execute the conditional code.
Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
// We actually only want to execute this code once like an if statement,
// not while the condition is true, so break out of the loop.
break;
}
In your original post, you are using a do while
loop rather than a while
loop. You should keep in mind that the do while
executes once for certain, regardless of whether its condition is true. It only checks the condition to see whether it should run additional times. The while
loop, on the other hand, checks the condition before executing at all, which means you can substitute an if
statement with it.
You should keep in mind that this is a bad practice. If you want to execute code depending on a certain condition, use an if statement. If you want to execute code repeatedly a certain number of times or while some condition is true, then use a loop.