具有两个变量的for循环何时终止? [英] When does the for loop with two variables terminate?

查看:133
本文介绍了具有两个变量的for循环何时终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(i = 0,j = 0; i< = 20,j< = 30; i ++,j ++)

我们何时退出此循环。

当1个条件为假或循环继续直到两个条件都变为假时?



我尝试了什么:



for(i = 0,j = 0; i< = 20,j< = 30; i ++,j ++)

我们何时退出此循环。

当1条件为假或循环继续直到两个条件都变为假时?

for(i=0,j=0;i<=20,j<=30;i++,j++)
when do we exit this loop.
when either 1 condition is false or the loop continues till both the conditions become false??

What I have tried:

for(i=0,j=0;i<=20,j<=30;i++,j++)
when do we exit this loop.
when either 1 condition is false or the loop continues till both the conditions become false??

推荐答案

只需创建并执行一个程序使用循环,看看会发生什么。



或者查找:

内置逗号运算符在其他运营商 - cppreference.com [ ^ ]或逗号运算符 - 维基百科 [ ^ ]。
Just create and execute a program using the loop and see what happens.

Or look it up:
"Built-in comma operator" at Other operators - cppreference.com[^] or Comma operator - Wikipedia[^].


为什么不亲自尝试?

Why don't you try it yourself?
// Example program
#include <iostream>
#include <string>

using namespace std;

int main()
{
  int i, j;
  for(i=0,j=0;i<=20,j<=30;i++,j++) {
  }
  
  cout << "Loop terminated. " << endl
        << "i: " << i << endl
        << "j: " << j << endl;
}



无论输出如何,警告都说,


Regardless of output, the warning came saying,

10:16: warning: left operand of comma operator has no effect [-Wunused-value]



导致输出为,


That caused output to be,

Loop terminated. 
i: 31
j: 31



这意味着,它只检查最右边的条件 j< = 30 。每次迭代也递增 i 所以它们都是31。


Which means, it only checked for right-most condition j <= 30. Each iteration also incremented i so they are both 31.


有一种简单的方法,知道:试试吧!

使用调试器查看代码执行情况,您将知道答案。

规则是1个循环,1个变量和1个条件。如果你想读取类似2D矩阵的东西,那就是2个嵌套循环。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
There is an easy way, to know: just try!
Use the debugger to see your code execute and you will know the answer.
The rule is 1 loop, 1 variable and 1 condition. If you want to read something like a 2D matrix, it is 2 nested loops.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于具有两个变量的for循环何时终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆