5

There is obviously something missing in my understanding of the for-loop. Part of my code looks like this:

int height;
do
{
    printf("Height?: ");
    height = GetInt();
}
while (height < 0 || height > 23);

// build pyramid
for (height; height > 0; height--)
{
    // print spaces
    for (int rows = height - 1; rows > 0; rows--)
    {
        string space = ".";
        printf("%s", space);
    }

When I try to compile I get the following:

m13.c:17:7: error: expression result unused [-Werror,-Wunused-value]
  for(height; height > 0; height--)
      ^~~~~~
1 error generated.

Can someone point out my misunderstanding? The int height seems to be used from this point in my code but the error says unused.

2 Answers 2

8

In a for loop, the first item is the initialization. You are saying

for (height; height>0; height--)

and the compiler is expecting that first item to be an initialization but you have simply given it a variable.

There are 2 ways around that.

One way:

for (int i = height; i>0; i--)

and then, in your inner loop, use i rather than height.

Or, simpler:

for ( ; height>0; height--)

where you just leave off the initializing step, because you already have the value you want.

1
  • Thanks, Kiwi. I had just read in an online tutorial this morning about the empty initialization option. Commented Aug 22, 2014 at 15:27
0

The error message expression result unused means that you're retrieving or calculating a value that you haven't used, e.g. by storing it in a variable, doing further calculations, or evaluating it in a conditional statement.

When you write:

for(height; height > 0; height--)
{
    \\ loop body
}

You're trying to use the variable height as the control variable for your loop. This is not stylistically appropriate; you should be using a dedicated control variable, which is to say, one that only exists during the scope of the loop and is only used to control the progression of the loop.

You've done this properly in the inner loop, where your control variable is rows. But in the outer loop, when you reference height you are retrieving whatever value is stored in that variable and just tossing it at the loop definition, like here! Have an int! It has no idea what to do with that int; it wants to initialize a control variable.

If you did use height this way, you'd be altering its value with the loop update statement. At that point it's no longer storing the value you declared it to store; if I enter 5 for the height of my tower, there's no reason your height variable should change unless I enter a different number. Just because you've already printed out part of the tower doesn't mean its height has changed.

So to fix this, declare a loop control variable that you initialize with the value of height. Something like this:

for(int i = height; i > 0; i--)
{
    \\ loop body
}

Now you're using the value that you retrieved from height by assigning it to i. That's what the compiler is looking for.

You would get this same error from any of these lines:

height;
5;
(494 % 18) - (2 * 1.12);
1
  • Very clear explanation. Thank you, Thomas. Counter-examples always help for newbies. Commented Aug 22, 2014 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .