Looping 10x input, using and for while loop in C #?

I am new to the C # basic course and it’s hard for me to get a job. I built a basic calculator and it works great. Now I had to add a new button called "Amount", which will take input from one of my fields (number1Txtbox) and add it to myself 10 times through the loop.

I poured the pages of my C # book and cannot understand what it is. I figured out how to initialize a loop with a counter, etc., I just can't get this to work for life.

I was told to use a for loop, and then switch to a while loop. This makes no sense to me; I suggested that I could do this with a for loop. So my question is:

1) Do I even need to switch to a while loop to do this?
2) What am I doing wrong?

Here is what I have so far, and it just makes my program freeze when I try to press the sum button after putting the number in the text box:

private void sumBtn_Click(object sender, EventArgs e) { int counter; int loopAnswer; int number1; number1 = int.Parse(number1Txtbox.Text); for (counter = 1; counter <= 10; counter++) { loopAnswer = number1 + number1; do { loopAnswer = loopAnswer + number1; } while (counter <= 10); equalsBox.Text = loopAnswer.ToString(); } } 

Thanks guys!

+4
source share
7 answers

You mix things. You either do this:

 private void sumBtn_Click(object sender, EventArgs e) { int counter; int loopAnswer = 0; int number1 = int.Parse(number1Txtbox.Text); for (counter = 1; counter <= 10; counter++) { loopAnswer += number1; //same as loopAnswer = loopAnswer + number1; } equalsBox.Text = loopAnswer.ToString(); } 

or that:

 private void sumBtn_Click(object sender, EventArgs e) { int counter = 1; int loopAnswer = 0; int number1 = int.Parse(number1Txtbox.Text); do { loopAnswer += number1; //same as loopAnswer = loopAnswer + number1; counter++; } while (counter <= 10); equalsBox.Text = loopAnswer.ToString(); } 

In addition, the final answer ( equalsBox.Text = loopAnswer.ToString(); ) should be outside the loop.

+7
source

It freezes because when it enters the do while , counter never changes. If it has never changed, counter <= 10 always true, so you get an infinite loop. He was stuck there.

 private void sumBtn_Click(object sender, EventArgs e) { //these should default to 0, but we should to it explicitly, just in case. int loopAnswer = 0; int number1; if(int.TryParse(number1Txtbox.Text, out number1) { for (counter = 1; counter <= 10; counter++) { loopAnswer += number1; } equalsBox.Text = loopAnswer.ToString(); } else equalsBox.Text = "Not A Number"; } 

TryParse is just a good practice here. He takes care of a situation where you will have text input. You can also use the try catch .

0
source
 private void sumBtn_Click(object sender, EventArgs e) { int counter; int loopAnswer = 0; int number1; number1 = int.Parse(number1Txtbox.Text); for (counter = 1; counter <= 10; counter++) { loopAnswer += number1; } equalsBox.Text = loopAnswer.ToString(); } 
0
source

Your program freezes because

  do { loopAnswer = loopAnswer + number1; } while (counter <= 10); 

Does not update the counter variable at all. Therefore, the counter will never reach 10, so this cycle will never exit.

to summarize in while loop do this

 counter = 1; do { loopAnswer += number1; counter++; } while(counter <= 10); 
0
source

Your inner while loop runs endlessly because the counter never increments, so your program freezes. Set a breakpoint on the line to be able to debug your program and better understand how loops work.

To solve this problem, you need only one loop, definitely not a nested loop. It doesn't matter what kind of looping mechanism you use.

0
source

This code causes an infinite loop (this is the cause of the freeze):

 for (counter = 1; counter <= 10; counter++) { loopAnswer = number1 + number1; do { loopAnswer = loopAnswer + number1; } while (counter <= 10); equalsBox.Text = loopAnswer.ToString(); } 

Infact here you loop from 1 to 10, and for each iteration you do loopAnswer = loopAnswer + number1; until the condition counter <= 10 becomes false. But this never happens since, as in your case, while the counter variable does not change, and therefore the program remains forever in the first iteration.

I think you need to get rid of the internal do-while and set equalsBox.Text = loopAnswer.ToString(); outside the for loop.

0
source

For example, suppose that number1 = 4. When you execute the line loopAnswer = number1 + number1; , the resulting loopAnswer value loopAnswer always be 8. If you want loopAnswer increase, you should use loopAnswer = loopAnswer + number1; or shorthand syntax loopAnswer += number1;

Regarding the use of the for loop versus do-while , I assume that it is not a question of using both loops at the same time, it is a question of using a for loop to illustrate a concept and then switch to using a do-while to illustrate a concept.

You can complete this exercise with a for loop as follows:

 for (counter = 1; counter <= 10; counter++) { loopAnswer += number1; } equalsBox.Text = loopAnswer.ToString(); 

You can also perform the same functionality using a do-while loop as follows:

 int counter = 1; do { loopAnswer += number1; counter++; } while (counter <= 10); equalsBox.Text = loopAnswer.ToString(); 
0
source

Source: https://habr.com/ru/post/1402312/


All Articles