adelebeth
adelebeth

Reputation: 11

Possible Empty statement error in a loop

This is my first loop assignment, so I am struggling to find the error, but the code looks like this:

  //Loop
        for (yearDisp == startYr; yearDisp <= endYr; yearDisp++); <-EMPTY STATEMENT ERROR
        {
            listBoxDisp.Items.Add("Year:" + yearDisp.ToString());
            yearDisp = yearDisp + 1;

I might be putting the whole thing together wrong.. I need to create something like this: User enters two years (ex: 1988 and 2022) and when I hit the "GO" button, the loop needs to display each year in the listbox starting with 1988, and 1999, 2000, etc all the way to 2022. Then it needs to stop.

Where am I going wrong, and why it that semicolon creating a "possible empty statement error?

Thanks in advance.

Upvotes: 0

Views: 49

Answers (1)

B.K.
B.K.

Reputation: 10162

  1. Get rid of that semicolon you pointed out. Semicolon marks the end of a statement... and you don't want your for loop to end there, otherwise it's empty. That's why you get that error.
  2. yearDisp == startYr should be yearDisp = startYr. It's an assignment not an equality check.
  3. Add a curly brace at the end of your for loop.
  4. I am not sure if it's by design, but you're iterating your yearDisp twice. Once by yearDisp++ and second time by yearDisp = yearDisp + 1. If you only mean to do it once, get rid of one of them.

    for (yearDisp = startYr; yearDisp <= endYr; yearDisp++)
    {
        listBoxDisp.Items.Add("Year:" + yearDisp.ToString());
        //yearDisp = yearDisp + 1; <-- may be a design error, do you need this one?
    }
    

Upvotes: 2

Related Questions