int i,j;
for(i=0;j=10;j>=0;i<10;i++;j--){
printf("%d %d",i,j);
}
It brings error while executing, how to rectify it and what is the correct syntax for using multiple iterators in for loop
int i,j;
for(i=0;j=10;j>=0;i<10;i++;j--){
printf("%d %d",i,j);
}
It brings error while executing, how to rectify it and what is the correct syntax for using multiple iterators in for loop
A for
loop has the following syntax:
for ( expression ; expression ; expression )
There are 3 expressions separated by semicolons. You have 6 expressions separated by semicolons. That's invalid syntax.
You should write it as follows:
for(i=0,j=10; j>=0 && i<10; i++,j--)
For the first expression, separate the two assignments with the comma operator. Similarly for the third expression. For the second, you want both conditionals to be true, so separate them with the logical AND operator &&
.
Also, the error you got was not while executing but while compiling.
Change for(i=0;j=10;j>=0;i<10;i++;j--){}
to for(i=0, j=10; j>=0 && i<10; i++,j--){}
You can use comma operator to separate statements in initialization and limit condition for both variables. See below.
int i,j;
for(i=0,j=10;j>=0 && i<10;i++,j--){
printf("%d %d",i,j);
}
To see Comma operator in the specs at following section
6.5.17 Comma operator Syntax expression:
assignment-expression
expression , assignment-expression