简体   繁体   中英

For loop in C for reading number of sequence from text file

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.

  • The first part of the loop is empty because it's not needed ( for(;;) is also valid).
  • The second part ( count>0; ) is the stop condition as you would expect
  • The third part is what is done in the end of each iteration, and in this case - that's 2 things (can be 0 commands, or any other number of commands): count--, j++

A for loop can be used with 0,1 or 2 of it parts used, meaning all of these options are OK:

  1. for(;;)
  2. for(int i;;) only declaration. A bit odd, but valid
  3. for(int i=0; i < 10;) also valid, but this could be an infinite loop
  4. 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)
  5. for(int i=0;i<10;i++) traditional
  6. for(;i<10;i++) valid if i is declared elsewhere (it also should be defined...)
  7. for(;i<10;) only stop condition, odd, but valid
  8. 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:

  1. int i = 0, j = 0; both will execute. No evaluation relevant
  2. TRICKY PART 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
  3. 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:

  • do nothing (; ;
    this is where usually the loop control variables are initialised,
    but if that is already done, it is fine to do nothing
  • start looping
    • if count is great than 0, count>0; , then do the body,
      else continue after the body
    • then decrement count and increment j count--, j++)
  • loop

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM