简体   繁体   中英

Looping 10 times adding input, with for and do while loop in C#?

I'm new taking a basic C# course and I'm having trouble getting an assignment to work. I built a basic calculator and it works fine. Now I had to add a new button called "Sum" that will take an input from one of my boxes (number1Txtbox) and add it to itself 10 times through a loop.

I poured through pages of my c# book and can't figure this one out. I figured out how to initialize the loop with the counter etc, I just can't get this to work for the life of me.

I was told to use a for loop, then switch to a do while loop. Which doesn't really make sense to me, I assumed I could do this with just a for loop. So my question is:

1) Do I even need to switch to a do 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 attempt to hit the sum button after putting a number in the textbox:

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!

You mixing 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 this:

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

}

Also, the final answer ( equalsBox.Text = loopAnswer.ToString(); ) should be out of the loop.

It freezes because when it enters the do while loop, the counter is never changed. If it's never changed, counter <= 10 is always true so you get an infinite loop. It is 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";
}

The TryParse here is just good practice. It takes care of a situation where you would have text input. A try catch block could also be used.

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

}

Your program freezes because

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

Doesn't update the counter variable at all. Therefore counter is never going to reach 10 so this loop is never going to exit.

to sum in a while loop do this

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

Your inner loop (while) is running endlessly because counter is never increased, that's why your program hangs. Set a breakpoint on a line to be able to debug your program and gain a better understanding of how loops work.

To solve this assignment you only need one loop, definitly not nested loops. It doesnt matter what kind of looping mechanism you use.

This code cause an infinite loop (that is the reason 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're looping from 1 to 10, and for each iteration you perform loopAnswer = loopAnswer + number1; until the condition counter <= 10 becomes false. But that never happens since in your do-while the counter variable doesn't change and so the program remains forever in the first iteration.

I think you should get rid of the inner do-while and put equalsBox.Text = loopAnswer.ToString(); outside the for-loop.

For the sake of examples, let's say that number1 = 4. When you execute the line loopAnswer = number1 + number1; the resulting value of loopAnswer will always be 8. If you wanted loopAnswer to increment, then you should use loopAnswer = loopAnswer + number1; , or the shorthand syntax loopAnswer += number1;

Regarding the use of a for loop versus a do-while , I'm guessing that it's not a matter of using both loops at the same time, it's a matter of using a for loop to illustrate the concept and then switching to use a do-while loop to illustrate the concept.

You could complete this exercise using a for loop like this:

for (counter = 1; counter <= 10; counter++)  
{  
    loopAnswer += number1; 
}

equalsBox.Text = loopAnswer.ToString();  

You could also accomplish the same functionality using a do-while loop like this:

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

equalsBox.Text = loopAnswer.ToString();  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM