pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 C++ 中,為什么我不能像這樣編寫 for() 循環:

In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
本文介紹了在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

或者,在 for 循環中聲明多個變量是禁止的"?!

or, "Declaring multiple variables in a for loop ist verboten" ?!

我的原始代碼是

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i2 = i * i ) {

我想遍歷第一個這么多方格,并且想要數字和它的方格,停止條件取決于方格.這段代碼似乎是最清晰的意圖表達,但它是無效的.我可以想到十幾種方法來解決這個問題,所以我不是在尋找最佳選擇,而是為了更深入地了解為什么這是無效的.如果愿意,可以學習一些語言法律方面的知識.

I wanted to loop through the first so-many squares, and wanted both the number and its square, and the stop condition depended on the square. This code seems to be the cleanest expression of intent, but it's invalid. I can think of a dozen ways to work around this, so I'm not looking for the best alternative, but for a deeper understanding of why this is invalid. A bit of language lawyering, if you will.

我年紀大了,還記得你必須在函數開始時聲明所有變量的時候,所以我很感激

I'm old enough to remember when you had to declare all your variables at the start of the function, so I appreciate the

for( int i = 0; ....

語法.閱讀它看起來像在 for() 語句的第一部分中只能有一個類型聲明.所以你可以這樣做

syntax. Reading around it looks like you can only have one type declaration in the first section of a for() statement. So you can do

for( int i=0, j=0; ...

甚至略帶巴洛克風格

for( int i=0, *j=&i; ...

但不是對我敏感的

for( int i=0, double x=0.0; ...

有人知道為什么嗎?這是 for() 的限制嗎?或者對逗號列表的限制,例如逗號列表的第一個元素可以聲明一個類型,但不能聲明另一個?以下逗號的使用是否與 C++ 的不同語法元素不同?

Does anyone know why? Is this a limitation of for()? Or a restriction on comma lists, like "the first element of a comma list may declare a type, but not the other? Are the following uses of commas distinct syntactical elements of C++?

(A)

for( int i=0, j=0; ...

(乙)

int i = 0, j = 0;

(C)

 int z;
 z = 1, 3, 4;

那里有大師嗎?

根據我得到的好評,我想我可以尖銳地提出這個問題:

Based on the good responses I've gotten, I think I can sharpen the question:

在 for 語句中

for( X; Y; Z;) {..... }

什么是 X、Y 和 Z?

what are X, Y and Z?

我的問題是關于 C++,但我沒有很好的 C++ 參考.在我的 C 參考(Harbison and Steele 4th ed, 1995)中,它們都是三個表達式,我的 gcc 需要 C99 模式才能使用 for( int i = 0;

My question was about C++, but I don't have a great C++ refrence. In my C reference (Harbison and Steele 4th ed, 1995), they are all three expressions, and my gcc requires C99 mode to use for( int i = 0;

在 Stroustrup,第 6.3 節中,for 語句的語法如下

In Stroustrup, sec 6.3, the for statement syntax is given as

for( for-init-statement; condition; expression ) 語句

for( for-init-statement; condition; expression ) statements

因此,C++ 有一個專門用于 for() 中的第一個子句的特殊語法語句,我們可以假設它們具有除表達式之外的特殊規則.這聽起來有效嗎?

So C++ has a special syntactic statement dedicated to the first clause in for(), and we can assume they have special rules beyond those for an expression. Does this sound valid?

推薦答案

int i = 1, double i2 = 0; 不是有效的聲明語句,因此不能在 for 語句.如果語句不能獨立于 for 之外,那么它就不能在 for 語句內使用.

int i = 1, double i2 = 0; is not a valid declaration statement, so it cannot be used inside the for statement. If the statement can't stand alone outside the for, then it can't be used inside the for statement.

關于逗號運算符的問題,選項A"和B"是相同的,并且都是有效的.選項C"也有效,但可能不會達到您的預期.z 將被賦值為 1,并且語句 34 實際上不會做任何事情(你的編譯器會可能會警告您沒有效果的語句"并優化它們).

Regarding your questions about comma operators, options 'A' and 'B' are identical and are both valid. Option 'C' is also valid, but will probably not do what you would expect. z will be assigned 1, and the statements 3 and 4 don't actually do anything (your compiler will probably warn you about "statements with no effect" and optimize them away).

更新:為了解決您編輯中的問題,以下是 C++ 規范(第 6.5 節)定義了 for:

Update: To address the questions in your edit, here is how the C++ spec (Sec 6.5) defines for:

for ( for-init-statement condition(opt) ; expression(opt) ) statement

它進一步將 for-init-statement 定義為 expression-statementsimple-declaration.conditionexpression 都是可選的.

It further defines for-init-statement as either expression-statement or simple-declaration. Both condition and expression are optional.

for-init-statement 可以是任何有效的 expression-statement(例如 i = 0;)或 simple-declaration(如int i = 0;).根據規范,語句 int i = 1, double i2 = 0; 不是有效的 simple-declaration,因此與 一起使用是無效的.作為參考,simple-declaration 被定義為(在第 7 節中):

The for-init-statement can be anything that is a valid expression-statement (such as i = 0;) or simple-declaration (such as int i = 0;). The statement int i = 1, double i2 = 0; is not a valid simple-declaration according to the spec, so it is not valid to use with for. For reference, a simple-declaration is defined (in Section 7) as:

attribute-specifier(opt) decl-specifier-seq(opt) init-declarator-list(opt) ;

其中 decl-specifier-seq 將是數據類型加上諸如 staticexterninit-declarator-list 之類的關鍵字 將是一個逗號分隔的聲明符列表及其可選的初始值設定項.嘗試將多個數據類型放入同一個 simple-declaration 實質上是在編譯器需要 init-declarator-list<的地方放置了一個 decl-specifier-seq/代碼>.看到這個元素不合適會導致編譯器將該行視為格式錯誤.

where decl-specifier-seq would be the data type plus keywords like static or extern and init-declarator-list would be a comma-separated list of declarators and their optional initializers. Attempting to put more than one data type in the same simple-declaration essentially places a decl-specifier-seq where the compiler expects a init-declarator-list. Seeing this element out of place causes the compiler to treat the line as ill-formed.

規范還指出,for 循環等效于:

The spec also notes that the for loop is equivalent to:

{
    for-init-statement
    while ( condition ) {
        statement
        expression ;
    }
}

where condition 如果省略,則默認為true".考慮這種擴展"形式可能有助于確定給定的語法是否可以與 for 循環一起使用.

where condition defaults to "true" if it is omitted. Thinking about this "expanded" form may be helpful in determining whether a given syntax may be used with a for loop.

這篇關于在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
How to break out of a loop from inside a switch?(如何從交換機內部跳出循環?)
主站蜘蛛池模板: 精密模具制造,注塑加工,吹塑和吹瓶加工,EPS泡沫包装生产 - 济南兴田塑胶有限公司 | 香港新时代国际美容美发化妆美甲培训学校-26年培训经验,值得信赖! | 防锈油-助焊剂-光学玻璃清洗剂-贝塔防锈油生产厂家 | TPU薄膜_TPU薄膜生产厂家_TPU热熔胶膜厂家定制_鑫亘环保科技(深圳)有限公司 | 煤棒机_增碳剂颗粒机_活性炭颗粒机_木炭粉成型机-巩义市老城振华机械厂 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 法兰螺母 - 不锈钢螺母制造厂家 - 万千紧固件--螺母街 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 深圳市人通智能科技有限公司| 颚式破碎机,圆锥破碎机,制砂机-新乡市德诚机电制造有限公司 | 中央空调温控器_风机盘管温控器_智能_液晶_三速开关面板-中央空调温控器厂家 | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 | 铝镁锰板_铝镁锰合金板_铝镁锰板厂家_铝镁锰金属屋面板_安徽建科 | 碎石机设备-欧版反击破-欧版颚式破碎机(站)厂家_山东奥凯诺机械 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 长沙一级消防工程公司_智能化弱电_机电安装_亮化工程专业施工承包_湖南公共安全工程有限公司 | 鑫达滑石-辽宁鑫达滑石集团 | 数年网路-免费在线工具您的在线工具箱-shuyear.com | 北京遮阳网-防尘盖土网-盖土草坪-迷彩网-防尘网生产厂家-京兴科技 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 液压中心架,数控中心架,自定心中心架-烟台恒阳机电设计有限公司 行星搅拌机,双行星搅拌机,动力混合机,无锡米克斯行星搅拌机生产厂家 | 微动开关厂家-东莞市德沃电子科技有限公司 | 大白菜官网,大白菜winpe,大白菜U盘装系统, u盘启动盘制作工具 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | HDPE储罐_厂家-山东九州阿丽贝防腐设备 | 微动开关厂家-东莞市德沃电子科技有限公司 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 河南生物显微镜,全自动冰冻切片机-河南荣程联合科技有限公司 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 篷房|仓储篷房|铝合金篷房|体育篷房|篷房厂家-华烨建筑科技官网 知名电动蝶阀,电动球阀,气动蝶阀,气动球阀生产厂家|价格透明-【固菲阀门官网】 | 北钻固控设备|石油钻采设备-石油固控设备厂家 | 办公室家具公司_办公家具品牌厂家_森拉堡办公家具【官网】 | 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 广州昊至泉水上乐园设备有限公司 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 贵州成人高考网_贵州成考网|