简体   繁体   English

在C中循环以从文本文件中读取序列号

[英]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. 我在阅读有关C语言中的循环的内容时,发现了一段有趣的代码,我无法真正理解。 I would appreciate if someone could explain me this line: for (; count>0; count--, j++) 如果有人可以向我解释这一行,我将不胜感激: 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; 变量countsize_t类型, int i,j = 0FILE *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循环看起来像for(i = 0; i < N; i++)这也是使用它的有效方法。

  • The first part of the loop is empty because it's not needed ( for(;;) is also valid). 循环的第一部分为空,因为不需要它( for(;;)也有效)。
  • The second part ( count>0; ) is the stop condition as you would expect 第二部分( count>0; )是您期望的停止条件
  • 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++ 第三部分是每次迭代结束时要做的事情,在这种情况下,这就是两件事(可以是0个命令,也可以是任何其他数量的命令): 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循环可以使用它的0,1或2 部分 ,这意味着所有这些选项都可以:

  1. for(;;)
  2. for(int i;;) only declaration. for(int i;;)仅声明。 A bit odd, but valid 有点奇怪,但有效
  3. for(int i=0; i < 10;) also valid, but this could be an infinite loop for(int i=0; i < 10;)也有效,但这可能是无限循环
  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) for(int i=0; ; i++)也有效,但这可能是一个无限循环,因为不存在停止条件(循环内的break可以处理此情况)
  5. for(int i=0;i<10;i++) traditional for(int i=0;i<10;i++)传统
  6. for(;i<10;i++) valid if i is declared elsewhere (it also should be defined...) for(;i<10;i++)如果在其他地方声明了i则有效(也应定义...)
  7. for(;i<10;) only stop condition, odd, but valid for(;i<10;)仅停止条件,奇数,但有效
  8. for(;;i++) also valid if i is declared elsewhere, but this could be an infinite loop 如果i在其他地方声明,则for(;;i++)也有效,但这可能是无限循环

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循环的每个部分中可以使用任意数量的语句,因此您可能会遇到类似以下内容的情况:

    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: 它将执行每个语句,但仅评估最后一个语句,这意味着在循环的3个部分中的每个部分:

  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 . TRICKY PART i < 1, j < 5都将执行, 只有j < 5会被评估,这意味着循环停止条件是j < 5 i < 1 is written there, but that will not make the loop stop i < 1写在那里,但这不会使循环停止
  3. i++, j++ same as 1. Both will execute. i++, j++与1相同。两者都将执行。 No evaluation relevant 没有相关评估

And this is the output: 这是输出:

0       0
1       1
2       2
3       3
4       4

for (; count>0; count--, j++) means: for (; count>0; count--, j++)意思是:

  • 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; 如果count大于0,则count>0; , then do the body, ,然后做身体,
      else continue after the body 否则身体之后继续
    • then decrement count and increment j count--, j++) 然后递减计数并递增j count-- count--, j++)
  • loop

Since he already declared the variable he wish to increment, he does not redeclare it in the for loop. 由于他已经声明了要增加的变量,因此他不会在for循环中重新声明它。 He also change two variables at each turn of the loop, he decrement count and increment j. 他还在循环的每一圈更改两个变量,即递减计数和递增j。 sscanf definition if you need. sscanf定义(如果需要)。 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 . count>0执行statement It repeatly does count--, j++ where , is the comma operator . 它屡做count--, j++哪里,逗号运算符 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. 计算expression1 ,然后计算expression2并返回expression2的值作为整个表达式的值。

Since the value of count--,j-- is not used here, this is just doing count-- followed by j-- . 由于此处未使用count--,j--的值,因此只在做count--然后是j--

Every argument of a for -loop is optional therefore even for(;;) is valid and equivalent to while(1) . for loop的每个参数都是可选的,因此,即使for(;;)也有效,并且等效于while(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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