f42 f42 -4 years ago 108
C Question

Using a incrementation of a variable in a while

I don't have the right to use more than 25 lines for a function.
One of my function has just 26 lines so I want to simplified it.

char *name(int big, int small, int nb, char *tab)
{
while (big < nb)
{
small = 1;
while (small < nb)
{
....
printf("here are the other lines\n");
....
small++;
}
big++;
}
return (tab);
}


big and small are initialize at 1.
How could I include the small++ and big++ in the while condition?

I tried
while (big++ && big < nb)
, it doesn't work why ?
The only way I just found is
while ((big = big + 1) && big < nb)


Does anyone can explain why the first way doesn't work and if you have another solution ?

Thanks

Answer Source

You probably want this (two lines less than your solution):

char *name(int big, int nb, char *tab)
{
    while (big++ < nb)
    {
        int small = 1;
        while (small++ < nb)
        {
            ....
            printf("here are the other lines\n");
            ....
        }
    }
    return tab;
}

Disclaimer: this is untested code.

while (big++ && big < nb) doesn't work, because it is simply not the same thing as:

while (big < nb)
{
  ...
  big++;
}

With while (big++ && big < nb) you increment big prior to the test. In the original code big is incremented after the test.

BTW: while ((big = big + 1) && big < nb) is the same thing as while (big++ < nb).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download