简体   繁体   English

对于C中的循环混淆

[英]For loop confusion in C

This is a question from a practice test that I do not fully understand. 这是一个我不完全理解的练习测试中的问题。

For the code fragment 对于代码片段

int i = 0, j = 0, k = 0;
for (i=-1; i<=10; ++i){ 
    j = i; ++k; 
}

I am asked to find the values of the variables after executing the code. 我被要求在执行代码后找到变量的值。

The answers are: 答案是:

 i = 11 j = 10 k = 12

I don't understand how, can someone please help? 我不明白怎么样,有人可以帮忙吗?

Understanding the value of i after the loop is very simple, much simpler than the sorts of other answers here. 在循环之后理解i的值非常简单,比这里的其他答案简单得多。 The loop condition is i<=10 ... in order for the loop to terminate, that condition must be false . 循环条件是i<=10 ...为了使循环终止,该条件必须为 Clearly, the value of i that makes that false is 11. 显然,使该错误的i值为11。

The value of j at the end of the loop is the previous value of i , which is 10, and the value of k is the number of times the loop executed, which is 1 (for -1) + 1 (for 0) + 10 (for 1 thru 10) = 12. 循环结束时j的值是i的前一个值, i 10, k的值是循环执行的次数,即1(对于-1)+ 1(对于0)+ 10(1至10)= 12。

i must be <= 10, so it is 11 to exit the loop and inside the last iteration of the loop, i = 10 = j. 我必须<= 10,所以退出循环是11并且在循环的最后一次迭代中,i = 10 = j。 k is 1 after the first iteration, while i is -1. 第一次迭代后k为1,而i为-1。 Running through the loop, you'll see: 通过循环,你会看到:

k = 1, i = -1
k = 2, i = 0
k = 3, i = 1
k = 4, i = 2
k = 5, i = 3
k = 6, i = 4
k = 7, i = 5
k = 8, i = 6
k = 9, i = 7
k = 10, i = 8
k = 11, i = 9
k = 12, i = 10

Therefore k = 12 因此k = 12

Here are the steps: 以下是步骤:

  1. When the loop begins, all three variables are zero. 当循环开始时,所有三个变量都为零。
  2. The loop initializer sets i to minus 1. 循环初始值设定项将i设置为-1。
  3. Loop test: i <= 10 is true, so loop is entered. 循环测试: i <= 10为真,因此输入循环。
  4. Inside the loop, j is set to i , so j is also minus 1. 在循环内部, j设置为i ,因此j也是-1。
  5. k is incremented, so k becomes 1. k递增,因此k变为1。
  6. the iteration ends; 迭代结束; i is incremented because of the ++i , so i becomes 0. i因为++i i而递增,所以i变为0。
  7. Loop test: since i is zero, i <= 10 is true, so the loop is entered again. 循环测试:由于i为零, i <= 10为真,因此再次输入循环。

In this way, the loop continues, changing j , k , and i in that order. 以这种方式,循环继续,以该顺序改变jki So when i becomes 10, j will be 9 and k 11. At that point: 所以当i变成10时, j将是9和k 11.在那一点上:

  1. The loop is entered for the last time. 最后一次输入循环。
  2. j becomes 10 as well; j变为10; k becomes 12 k变为12
  3. Then i gets incremented to 11. The loop condition i <= 10 is false, and the loop terminates. 然后i增加到11.循环条件i <= 10为假,循环终止。

So i is 11. j is 10, k is 12 when the loop terminates. 所以i是11. j是10,当循环终止时k是12。

The key point is, after the first pass, every time the loop is entered, j is one less than i , and k is one greater than i . 关键点是,在第一次传递之后,每次进入循环时, ji小1, ki When the loop terminates, this is still the case. 当循环终止时,情况仍然如此。

for (i=-1; i<=10; ++i){ 
    j = i; ++k; 
}

Here is the loop : 这是循环:

i = i +1;     <-------+
   |                  |
check condition!------|--+
   |                  |  |
j = i;                |  |
   |                  |  | 
  k++;----------------+  |
   |                     |
   +<--------------------+
   |
other code

at last loop 在最后一个循环

  i = 10
   condition == true
   j = 10;
   k = 12;

Then 然后

i= i+1 means i = 11 but the condition show false! i= i+1表示i = 11但条件显示为假! loop end. 循环结束。

Take three variables separate. 将三个变量分开。 You can see the variable k would be incremented , the number of times the loop is executed. 您可以看到变量k将递增,循环执行的次数。 The no. 没有。 of time sit would be executed from -1 to 10 it would have done 12 iterations 时间坐标将从-1到10执行,它将完成12次迭代

k = 1,  i = -1,  j=-1
k = 2,  i = 0,   j=0
k = 3,  i = 1,   j=1
k = 4,  i = 2,   j=2
k = 5,  i = 3,   j=3
k = 6,  i = 4,   j=4
k = 7,  i = 5,   j=5
k = 8,  i = 6,   j=6
k = 9,  i = 7,   j=7
k = 10, i = 8,   j=8
k = 11, i = 9,   j=9
k = 12, i = 10,  j=10

After This i has reached its limit, but it will first increment and then check, hence i=11, k=12 and j to a one less than the value of i ie j= 10 在此之后我已达到其极限,但它将首先递增然后检查,因此i = 11,k = 12并且j比一个小于i的值,即j = 10

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

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