0

I'm a beginner in programming so I would like a simple answer :)

I have a for loop with multiple conditions, which prints out two arrays. It works, but i get a warning and a red line under for(). Why is it so and how can I avoid it? I'm writing it in C and I use a Geany compiler in Ubuntu. :)

for((i=LEN-1) && (j=1); (i>=LEN-3) && (j<=PODIUM); i-- && j++)  
{       
  printf("%d. koht: %s tulemusega %f\n", j, voist[i], tul[i]);      
}
Jaan
  • 3
  • 3
  • 1
    you need to change `(i=LEN-1) && (j=1)` to `i=LEN-1,j=1` and `i-- && j++` to `i--,j++` – SHR Nov 28 '13 at 14:05

2 Answers2

0

This warning is because of the return value of (i=LEN-1) && (j=1) which is bot used further.To avoid the warning, try this

int temp;
... 

temp = (i=LEN-1) && (j=1);

for(; (i>=LEN-3) && (j<=PODIUM); i-- && j++)  
{  

     ....

     temp = (i=LEN-1) && (j=1);
} 
haccks
  • 104,019
  • 25
  • 176
  • 264
0
for(i=LEN-1,j=1 ; (i>=LEN-3) && (j<=PODIUM); i--, j++) 

EDIT: It works because this is the correct syntax. you don't need to use and operator to combine two initializations or two increments. you can just use the ,

SHR
  • 7,940
  • 9
  • 38
  • 57
  • this solution worked. but why does it work? can you explain? :) – Jaan Nov 28 '13 at 14:14
  • The compiler was storing the boolean result in some temp variable internally , which was never used . When we remove the boolean conditions for first , last arguments of for it solves the purpose ! – Srikanth Nov 28 '13 at 14:18