久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C++ 'for' 循環中放置兩個增量語句

How do I put two increment statements in a C++ #39;for#39; loop?(如何在 C++ for 循環中放置兩個增量語句?)
本文介紹了如何在 C++ 'for' 循環中放置兩個增量語句?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想在 for 循環條件中增加兩個變量而不是一個.

I would like to increment two variables in a for-loop condition instead of one.

比如:

for (int i = 0; i != 5; ++i and ++j) 
    do_something(i, j);

這是什么語法?

推薦答案

一個常見的習慣用法是使用 逗號運算符 計算兩個操作數,并返回第二個操作數.因此:

A common idiom is to use the comma operator which evaluates both operands, and returns the second operand. Thus:

for(int i = 0; i != 5; ++i,++j) 
    do_something(i,j);

但它真的是逗號運算符嗎?

寫完之后,一位評論者認為它實際上是 for 語句中的一些特殊語法糖,根本不是逗號運算符.我在 GCC 中進行了如下檢查:

But is it really a comma operator?

Now having wrote that, a commenter suggested it was actually some special syntactic sugar in the for statement, and not a comma operator at all. I checked that in GCC as follows:

int i=0;
int a=5;
int x=0;

for(i; i<5; x=i++,a++){
    printf("i=%d a=%d x=%d
",i,a,x);
}

我期望 x 獲取 a 的原始值,因此它應該為 x 顯示 5,6,7...我得到的是這個

I was expecting x to pick up the original value of a, so it should have displayed 5,6,7.. for x. What I got was this

i=0 a=5 x=0
i=1 a=6 x=0
i=2 a=7 x=1
i=3 a=8 x=2
i=4 a=9 x=3

但是,如果我將表達式括起來以強制解析器真正看到逗號運算符,我會得到這個

However, if I bracketed the expression to force the parser into really seeing a comma operator, I get this

int main(){
    int i=0;
    int a=5;
    int x=0;

    for(i=0; i<5; x=(i++,a++)){
        printf("i=%d a=%d x=%d
",i,a,x);
    }
}

i=0 a=5 x=0
i=1 a=6 x=5
i=2 a=7 x=6
i=3 a=8 x=7
i=4 a=9 x=8

最初我認為這表明它根本不像逗號運算符,但事實證明,這只是一個優先級問題 - 逗號運算符具有 最低可能的優先級,所以表達式 x=i++,a++ 被有效地解析為 (x=i++),a++

Initially I thought that this showed it wasn't behaving as a comma operator at all, but as it turns out, this is simply a precedence issue - the comma operator has the lowest possible precedence, so the expression x=i++,a++ is effectively parsed as (x=i++),a++

感謝所有的評論,這是一次有趣的學習經歷,我已經使用 C 多年了!

Thanks for all the comments, it was an interesting learning experience, and I've been using C for many years!

這篇關于如何在 C++ 'for' 循環中放置兩個增量語句?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: a免费视频 | 99色在线 | 黄色电影在线免费观看 | 日韩高清三区 | 91国产在线播放 | 日日摸夜夜添夜夜添特色大片 | 一级毛片播放 | 二区国产| 国产一区二区三区免费 | 日韩一区二区在线视频 | 精品少妇一区二区三区日产乱码 | 亚洲欧洲在线视频 | 国产欧美久久精品 | 欧洲免费视频 | av手机免费在线观看 | 精品国产乱码 | 欧美日韩综合一区 | 精品国偷自产在线 | av在线免费观看网站 | 久产久精国产品 | 在线国产视频观看 | 中文字幕一区在线观看视频 | 亚洲福利一区 | 国产一区二区在线免费观看 | 在线观看精品视频网站 | av手机免费在线观看 | 日韩中文字幕一区二区 | 久久99精品久久久久久青青日本 | 欧美精品一区三区 | 久草新在线 | 国产一区二区在线免费观看 | 色片在线观看 | 色婷婷久久久久swag精品 | www.日本三级 | 91偷拍精品一区二区三区 | 午夜小视频在线播放 | 天天射中文 | www.av在线| 色播久久 | 成人久草| 成人午夜免费在线视频 |