Sunday, 6 September 2015

Using two variables in for loop

 How to use two variables in for loop in C ?


Before answering this question here is an implementation which is wrong

for(i=0;j=0;i<n;j<m;i++;j++)
above implementation is syntactically wrong, the syntax of for loop is : 


         for( (initialisation) ; (condition)   ;  (increment/decrement) )


Now, the implementation :
for(i=0, j=0; i<0, j<0; i++, j++  )  is syntactically correct but this might not give you the correct output because mostly the required of your iteration is the both the conditions (i<0 and j<0 ) must be checked for simultaneously, which the above implementation fails to check.
So more precisely you can use :


for( i=0, j=0; i<0 && j<0; i++, j++  )


to ensure that both the conditions are true to continue in the loop.

To see more such questions click the link below:More Questions

No comments:

Post a Comment