Two variable in a for loop

Hi I'm new to c program here :) may I know how can I use two variable for one for loop statement can I just declare like this. However I saw some online people said that by declaring this way the system will take the second value and neglate the first value is this true ?

 
  for(sector=0,y=0;sector<4,y<5;sector++,y++
It depends what outcome you aim to achieve.
Instead of this:
 
sector<4,y<5
which will evaluate as
 
y<5

You might put
 
(sector<4)  && (y<5)
or
 
(sector<4)  || (y<5)
or
 
sector<4
or
 
y<5


Which of those is appropriate depends on what your intention would be. Try them and see, if not sure.

See Comma operator ( , )
http://www.cplusplus.com/doc/tutorial/operators/

Okay thank you chervil :)
take the second value and neglate the first value is this true ?
Yes, expressions separated by comma are evaluated but only the last will return a value. Thus sector<4 will be ignored. Note that what an expression within the comma list is depends on the precedence of the operators. The comma has a low precedence. See:

http://www.cplusplus.com/doc/tutorial/operators/
Also, based on previous code where variables sector any y were used, I'd suspect "none of the above" might be what is really needed.

More probably - and I'm guessing here since there clearly isn't enough information in this thread, you would want a nested loop -that is two individual loops, one contained inside the other
1
2
3
4
5
6
7
    for (sector=0; sector<4; sector++)
    {
        for (y=0; y<5; y++)
        {
       
        }
    }

... or possibly the inner and outer could be the other way around.
Float first,second,third
First=0;
Second=0;
Third=0;

For(sector=0&&y=0 ;sector<5&&y<4;sector++&&y ++)
{
temp= dataGDP[sector][y];
}
First=temp
Second=temp
Third=temp

If (temp>first)
{
Temp=first;
}
If(temp>second&&steno<second)
{
Temp=second;
}
If(temp>third&&temp<second)
{
Temp=third;
}

Printf(" the first is %.2f for %c" first,sector)

Printf(" the second is %.2f for %c" second ,sector)

Printf(" the third is %.2f for %c" third, sector)




Does this code display the TOP three value and sector ?
Topic archived. No new replies allowed.