FOR 循环中的逗号运算符是如何工作的?

发布于 2024-12-28 07:32:44 字数 270 浏览 3 评论 0原文

谁能解释一下 FOR 语句中的逗号运算符吗?

function funct_1(c){
    for (var a = x, e = y; 0 < c; ){ 
         var p = c/2;
         var c = c/10; // wtf, it is already defined as function argument!!
    }
}

另外,最后一个语句“a++”似乎丢失了。我从来没有见过这样的事情。这意味着什么?

Can anyone please explain the comma operator in FOR statement?

function funct_1(c){
    for (var a = x, e = y; 0 < c; ){ 
         var p = c/2;
         var c = c/10; // wtf, it is already defined as function argument!!
    }
}

Also, the last statement like "a++" seems to be missing. I have never seen anything like this. what does that mean?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温馨耳语 2025-01-04 07:32:44

逗号只是为多个声明添加分隔符。换句话说,您的 for 循环将 a 设置为等于 x,并将 e 设置为等于 >y

至于缺少increment语句,缺少它只是意味着for循环不会显式增加任何变量。

The comma just adds separation for multiple declarations. In other words, your for loop is setting a equal to x, as well as e equal to y.

As for the lack of the increment statement, the fact that it is missing just means that the for loop won't explicitly increment any variable.

仙女 2025-01-04 07:32:44

逗号只允许您在循环开始时初始化多个变量。缺少增量运算符意味着循环内必须有某个脚本最终满足终止条件,否则循环将永远不会完成。

The comma just allows you to initialise more than one variable at the start of the loop. And the missing increment operator means that there must be some script inside the loop that will eventually satisfy the termination condition, otherwise the loop would never complete.

飘逸的'云 2025-01-04 07:32:44

C、C++ 和 JavaScript(也许是 C#)中的逗号运算符的工作方式如下:

comma_operator(statement_1, statement_2) {
     execute statement_1
     return statement_2
}

因此,在循环中,它初始化两个整数值,ae,它们是分别设置为xy。没有增量,因为循环正在与 c 进行比较,c 可能是在循环内的某个位置设置的。

The comma operator in C, C++, and JavaScript (maybe C#) works like this:

comma_operator(statement_1, statement_2) {
     execute statement_1
     return statement_2
}

So, in your loop, it initializes two integer values, a and e, which are set to x and y, respectively. There is no increment because the loop is comparing against c, which is probably set somewhere inside the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文