I was reading about loops in C, I found an interesting line of code, which I could not really understand. I would appreciate if someone could explain me this line: for (; count>0; count--, j++)
But the whole code was like this:
while(getline(&line, &count, input) != -1)
{
for ( ; count>0; count--, j++)
sscanf(line, "%d", &array[i]);
i++;
}
The variable count
is size_t
type, int i,j = 0
and FILE *input;
for reading a sequence of numbers stored in text file.
Thanks in advance.
While a traditional for
loop looks like for(i = 0; i < N; i++)
this is also a valid way to use it.
for(;;)
is also valid). count>0;
) is the stop condition as you would expect count--, j++
A for
loop can be used with 0,1 or 2 of it parts used, meaning all of these options are OK:
for(;;)
for(int i;;)
only declaration. A bit odd, but valid for(int i=0; i < 10;)
also valid, but this could be an infinite loop for(int i=0; ; i++)
also valid, but this could be an infinite loop since no stop condition is present (a break
inside the loop can handle this) for(int i=0;i<10;i++)
traditional for(;i<10;i++)
valid if i
is declared elsewhere (it also should be defined...) for(;i<10;)
only stop condition, odd, but valid for(;;i++)
also valid if i
is declared elsewhere, but this could be an infinite loop Another thing that should be noted: since any number of statements can be used in each part of the for loop, you could have something like:
for (int i = 0, j = 0; i < 1, j < 5; i++, j++)
printf("%d\t%d\n", i, j);
Note that using ,
is tricky. It will execute each of the statements but only evaluate the last, meaning that in each of the 3 parts of the loop:
int i = 0, j = 0;
both will execute. No evaluation relevant i < 1, j < 5
both will execute BUT only j < 5
will be evaluated, meaning, the loop stop condition is j < 5
. i < 1
is written there, but that will not make the loop stop i++, j++
same as 1. Both will execute. No evaluation relevant And this is the output:
0 0
1 1
2 2
3 3
4 4
for (; count>0; count--, j++)
means:
(;
; count>0;
, then do the body, count--, j++)
Since he already declared the variable he wish to increment, he does not redeclare it in the for loop. He also change two variables at each turn of the loop, he decrement count and increment j. sscanf definition if you need. He basically read a file.
Let's examine the code line that confuses you.
for (; count>0; count--, j++){
statement
}
executes statement
while count>0
. It repeatly does count--, j++
where ,
is the comma operator . This operator does the following:
expression1, expression2
evalutes expression1
, then evaluates expression2
and returns the value of expression2
as the value of the whole expression.
Since the value of count--,j--
is not used here, this is just doing count--
followed by j--
.
Every argument of a for
-loop is optional therefore even for(;;)
is valid and equivalent to while(1)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.