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

為什么for循環的條件可以留空?

Why can the condition of a for-loop be left empty?(為什么for循環的條件可以留空?)
本文介紹了為什么for循環的條件可以留空?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

可能的重復:
for 和 while 循環中沒有循環條件

為什么允許 for 循環 中的條件為空?例如

Why is the condition in a for-loop allowed to be left empty? For example

for (;;)

編譯正常.為什么這個空表達式的計算結果為 true 而下面的

compiles fine. Why does this empty expression evaluate to true but the following

if () {}
while () {}

兩者都會失敗嗎?我想知道是否/為什么for循環是這種情況的一個例外.

will both fail? I want to know if/why the for-loop is an exception to this case.

推薦答案

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

這些是迭代語句

while(表達式)語句

while ( expression ) statement

do 語句 while ( 表達式 ) ;

do statement while ( expression ) ;

for ( 表達式 [opt] ; 表達式 [opt] ; 表達式 [opt] ) 語句

for ( expression [opt] ; expression [opt] ; expression [opt] ) statement

for ( 聲明表達式 [opt] ; 表達式 [opt] ) 語句

for ( declaration expression [opt] ; expression [opt] ) statement

while 循環旨在評估控制表達式before 每次執行循環,而 do 循環旨在評估after 每次執行.

The while loop was designed to evaluate the controlling expression before each execution of the loop and the do loop was designed to evaluate after each execution.

for 循環被設計為更復雜的迭代語句.

The for loop was designed as a more sophisticated iteration statement.

6.8.5.3 for 語句

6.8.5.3 The for statement

聲明

for ( clause-1 ; expression-2 ; expression-3 ) 語句

for ( clause-1 ; expression-2 ; expression-3 ) statement

行為如下:表達式 expression-2 是控制表達式,即在每次執行循環體之前評估.表達方式每次執行后,表達式 3 被評估為一個空表達式循環體.如果子句 1 是聲明,則任何它聲明的標識符是聲明的其余部分,而整個循環,包括其他兩個表達式;它是在首次評估控制之前的執行順序表達.如果子句 1 是表達式,則將其計算為 void控制表達式的第一次計算之前的表達式.

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identi?ers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the ?rst evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the ?rst evaluation of the controlling expression.

子句 1 和表達式 3 都可以省略.一個省略表達式 2 被一個非零常量替換.

Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

規范允許表達式 2(循環的條件)被省略,并由一個非零常量代替.這意味著 for 循環將無限期地繼續執行.

The specification allows expression-2, the condition of the loop, to be omitted and is replaced by a nonzero constant. This means that the for loop will continue to execute indefinitely.

這對于允許無止境迭代的簡單語法很有用.

This is useful for allowing a simple syntax for iterating with no end.

for(int i = 0;;i++) { //do stuff with i }

這比編寫一個在循環外聲明變量然后在循環內遞增的 while(1) 循環要簡單得多.

That's much simpler to write and understand than writing a while(1) loop with a variable declared outside the loop and then incremented inside the loop.

然后,for 循環規范繼續允許您省略子句 1,以便您可以在其他地方聲明或初始化變量,并且您可以省略表達式 3,以便您不需要在每個循環完成時評估任何表達式.

The for loop specification then goes on to allow you to omit clause-1 so that you can declare or initialize variables elsewhere, and you can omit expression-3 so that you are not required to evaluate any expression upon completion of each loop.

for 循環是一個特例.while 循環和 do 循環是嚴格的,需要一個表達式,而 for 循環是一個靈活的迭代語句.

The for loop is a special case. The while loop and the do loop are strict and require an expression, but the for loop is a flexible iteration statement.

這篇關于為什么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 表達式中的變量聲明)
主站蜘蛛池模板: 转向助力泵/水泵/发电机皮带轮生产厂家-锦州华一精工有限公司 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 船用烟火信号弹-CCS防汛救生圈-船用救生抛绳器(海威救生设备) | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 宝鸡市人民医院 | 废气处理_废气处理设备_工业废气处理_江苏龙泰环保设备制造有限公司 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 贵阳用友软件,贵州财务软件,贵阳ERP软件_贵州优智信息技术有限公司 | 铸钢件厂家-铸钢齿轮-减速机厂家-淄博凯振机械有限公司 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 品牌设计_VI设计_电影海报设计_包装设计_LOGO设计-Bacross新越品牌顾问 | 水质传感器_水质监测站_雨量监测站_水文监测站-山东水境传感科技有限公司 | 超声骨密度仪-动脉硬化检测仪器-人体成分分析仪厂家/品牌/价格_南京科力悦 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 冷油器,取样冷却器,热力除氧器-连云港振辉机械设备有限公司 | 水平筛厂家-三轴椭圆水平振动筛-泥沙震动筛设备_山东奥凯诺矿机 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 机构创新组合设计实验台_液压实验台_气动实训台-戴育教仪厂 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 混合生育酚_醋酸生育酚粉_琥珀酸生育酚-山东新元素生物科技 | 线粒体膜电位荧光探针-细胞膜-标记二抗-上海复申生物科技有限公司 | VOC检测仪-甲醛检测仪-气体报警器-气体检测仪厂家-深恒安科技有限公司 | 小威小说网 - 新小威小说网 - 小威小说网小说搜索引擎 | 耐压仪-高压耐压仪|徐吉电气 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 无菌检查集菌仪,微生物限度仪器-苏州长留仪器百科 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 熔体泵_熔体出料泵_高温熔体泵-郑州海科熔体泵有限公司 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 气动隔膜泵-电动隔膜泵-循环热水泵-液下排污/螺杆/管道/化工泵「厂家」浙江绿邦 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 深圳天际源广告-形象堆头,企业文化墙,喷绘,门头招牌设计制作专家 | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 冷热冲击试验箱_温度冲击试验箱价格_冷热冲击箱排名_林频厂家 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 全自动五线打端沾锡机,全自动裁线剥皮双头沾锡机,全自动尼龙扎带机-东莞市海文能机械设备有限公司 |