while(1) 和 for( ; ; )有什么區(qū)別?
作者 |?strongerHuang 微信公眾號 |?嵌入式專
要回答這個問題,其實你各自編寫一段while(1) 和 for(;;)的代碼,編譯對比一下代碼大小和匯編文件,你就大概知道了。
while(1)和for(;;)語法表達
while( 表達式 ){語句}
其中:
表達式:是循環(huán)條件
語句:為循環(huán)體。

for(表達式1; 表達式2; 表達式3){語句}
1.先求解表達式1
2.求解表達式2
若其值為真(非0),則執(zhí)行for語句中指定的內(nèi)嵌語句,然后執(zhí)行下面第3)步;
若其值為假(0),則結束循環(huán),轉到第5)步。
3.求解表達式3
4.轉回上面第2)步繼續(xù)執(zhí)行。
5.循環(huán)結束,執(zhí)行for語句下面的一個語句。
微信搜索公眾號:程序員開源社區(qū),回復“資源”,獲取更多精品學習資料。

while(1)和for(;;)異同點
驗證while(1)和for(;;)差異
// filename: while.cint main(int argc, char const *argv[]){while(1){}return 0;}
// filename: for.cint main(int argc, char const *argv[]){for(;;){}return 0;}
gcc -S -o while.s while.cgcc -S -o for.s for.c
; filename: whiles.file "while.c".text.globl main.type main, @functionmain:.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6movl %edi, -4(%rbp)movq %rsi, -16(%rbp).L2:jmp .L2.cfi_endproc.LFE0:.size main, .-main.ident "GCC: (GNU) 9.3.0".section .note.GNU-stack,"",@progbits
; filename: for.s.file "for.c".text.globl main.type main, @functionmain:.LFB0:.cfi_startprocpushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16movq %rsp, %rbp.cfi_def_cfa_register 6movl %edi, -4(%rbp)movq %rsi, -16(%rbp).L2:jmp .L2.cfi_endproc.LFE0:.size main, .-main.ident "GCC: (GNU) 9.3.0".section .note.GNU-stack,"",@progbits
整理好了一份《Java大廠面試題》,累計 1098頁,涵蓋了Java技術的方方面面,記得收藏哈!
全網(wǎng)互聯(lián)網(wǎng)面試題,怎么領取? (回復?面試題?)加群看群公告領取
評論
圖片
表情


