4

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!

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
drowningincoffee
  • 93
  • 2
  • 3
  • 11
  • 2
    In addition to Adrians answer, the reason your program is freezing is because your do while loop has a condition that the counter has to be less than or equal to 10. The problem is that your do while loop is *inside* the loop that increases the counter. Which means do while can never finish executing, because the for loop (which increments the counter) can never finish a single iteration. – Brandon Mar 19 '12 at 20:38
  • @AnuragRanjhan, his first line says that it is an assignment. So I'm guessing yes. – Brandon Mar 19 '12 at 20:38
  • 3
    This is a good question for a brand new beginner programmer. Thank you for including your code and the fact that it was for homework. – David Mar 19 '12 at 20:40
  • 1
    When the assignment said "use a `for` loop, then switch to a `do...while` loop, they likely meant two different solutions so you could show you knew how to use both. – Adam V Mar 19 '12 at 20:40
  • Just a comment on switching to the do while loop. I believe they are asking you to change from a for loop to a do while loop so that you can gain some experience using both constructs and be better able to make decisions in the future about which loop construct to use. It is true that you can accomplish this with just a for loop, they are asking you to do the operation twice, once with for and once with do while. @Adrian has provided a clean answer with the for loop, you should take what has been presented and replace the for loop with the do while loop. – pstrjds Mar 19 '12 at 20:42

7 Answers7

7

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.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0

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.

Toast
  • 424
  • 2
  • 9
0
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();

}
SupremeDud
  • 1,371
  • 10
  • 18
0

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);
twain249
  • 5,666
  • 1
  • 21
  • 26
0

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.

Marcus
  • 2,470
  • 1
  • 22
  • 29
0

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.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
0

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();  
pmartin
  • 2,731
  • 1
  • 25
  • 30