简体   繁体   English

该变量的值如何在此循环之外递增?

[英]How is the value of this variable incremented outside of this loop?

I have a question for the value of j inside this nested loop. 我对此嵌套循环内的j的值有疑问。

    for (potentialSum=1; potentialSum<=m; potentialSum ++)
    {
         for (j=1;j<=n;j++)
         {
             if (potentialSum == 2) {
                 printf("j:%d in loop\n", j);
             }
         }

         C[potentialSum]=(j<=n) ? j : (-1);

         if (C[potentialSum] == -1) {
              printf("j:%d n:%d \n", j , n);
         }

    }

n = 0 and m = 25. n = 0,m = 25。

So when I run this loop with the aforementioned values for n and m, I get an output something like this: 因此,当使用上述的n和m值运行此循环时,将得到类似以下的输出:

j:1 in loop
j:2 in loop
j:3 in loop
j:4 in loop
j:5 in loop
j:6 in loop
j:7 in loop
j:8 n:7 // Outside of loop

My question is when/how does j get incremented to 8, if n=7 ? 我的问题是,如果n=7j何时/如何增加到8?

This only happens when potentialSum = 2 , for the complete code click here and for a copy of the input click here . 仅当potentialSum = 2时才会发生这种情况,对于完整的代码,请单击此处 ,对于输入的副本,请单击此处

Thanks for all the help in advance, I'm just really not seeing how j goes from 7 to 8 outside of the loop. 感谢预先提供的所有帮助,我真的没有看到j在循环外如何从7变为8。

for (j=1;j<=n;j++)   //where n is 7

for( declaration ; comparison(condition checking) , increment/decrement) for(声明;比较(条件检查),递增/递减)

after declaration, value is compared, and at the end its incrementing ( j++ ) 声明之后,比较值,最后将其递增( j++

when j=7 it will check condition j<=n which is true so it will go inside the loop. j=7 ,它将检查条件j<=n为真,因此它将进入循环内。 and at the it will increment j++ . 并且在它将增加j++
Now current value of j will become 8 . 现在j当前值将变为8 Next time it will check condition j<=n which is false so it will come out of the loop, but j will remain 8 . 下次它将检查条件j<=n为假,因此它将退出循环,但j仍为8

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

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