在C编程中与之相对?

有三个环在C: ,for,while和do-while。它们之间有什么区别?


例如,似乎几乎所有while语句都可以被for语句替换,对吗?那么,使用的好处是什么while?


婷婷同学_
浏览 406回答 3
3回答

一只萌萌小番薯

它们都是可以互换的。您可以选择一种类型,而永远只使用一种类型,但是通常对于给定任务而言,一种类型更方便。就像说“为什么要切换,您只能使用一堆if语句”一样-的确如此,但是如果它是一种常见的模式来检查变量的一组值,那么它具有语言功能,并且很容易阅读要做到这一点

肥皂起泡泡

如果强烈关注速度和性能,最好的方法是在汇编级别验证编译器生成的代码。例如,以下代码显示“ do-while”要快一些。这是因为“ do-while”循环未使用“ jmp”指令。顺便说一句,在此特定示例中,最糟糕的情况由“ for”循环给出。:))int main(int argc, char* argv[]){&nbsp; &nbsp; int i;&nbsp; &nbsp; char x[100];&nbsp; &nbsp; // "FOR" LOOP:&nbsp; &nbsp; for (i=0; i<100; i++ )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x[i] = 0;&nbsp; &nbsp; }&nbsp; &nbsp; // "WHILE" LOOP:&nbsp; &nbsp; i = 0;&nbsp; &nbsp; while (i<100 )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x[i++] = 0;&nbsp; &nbsp; }&nbsp; &nbsp; // "DO-WHILE" LOOP:&nbsp; &nbsp; i = 0;&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; x[i++] = 0;&nbsp; &nbsp; }&nbsp; &nbsp; while (i<100);&nbsp; &nbsp; return 0;}//“ FOR”循环:&nbsp; &nbsp; 010013C8&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],0&nbsp; &nbsp; 010013CF&nbsp; jmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wmain+3Ah (10013DAh)&nbsp; for (i=0; i<100; i++ )&nbsp; {&nbsp; &nbsp; &nbsp; x[i] = 0;&nbsp; &nbsp; 010013D1&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eax,dword ptr [ebp-0Ch]&nbsp; <<< UPDATE i&nbsp; &nbsp; 010013D4&nbsp; add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eax,1&nbsp; &nbsp; 010013D7&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],eax&nbsp; &nbsp; 010013DA&nbsp; cmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],64h&nbsp; <<< TEST&nbsp; &nbsp; 010013DE&nbsp; jge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wmain+4Ah (10013EAh)&nbsp; &nbsp; &nbsp;<<< COND JUMP&nbsp; &nbsp; 010013E0&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eax,dword ptr [ebp-0Ch]&nbsp; <<< DO THE JOB..&nbsp; &nbsp; 010013E3&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte ptr [ebp+eax-78h],0&nbsp; &nbsp; 010013E8&nbsp; jmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wmain+31h (10013D1h)&nbsp; &nbsp; &nbsp;<<< UNCOND JUMP&nbsp; }//“ WHILE”循环:&nbsp; i = 0;&nbsp; 010013EA&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],0&nbsp; while (i<100 )&nbsp; {&nbsp; &nbsp; &nbsp; x[i++] = 0;&nbsp; &nbsp; 010013F1&nbsp; cmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],64h&nbsp; &nbsp;<<< TEST&nbsp; &nbsp; 010013F5&nbsp; jge&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wmain+6Ah (100140Ah)&nbsp; &nbsp; &nbsp; <<< COND JUMP&nbsp; &nbsp; 010013F7&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eax,dword ptr [ebp-0Ch]&nbsp; &nbsp;<<< DO THE JOB..&nbsp; &nbsp; 010013FA&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte ptr [ebp+eax-78h],0&nbsp; &nbsp; 010013FF&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ecx,dword ptr [ebp-0Ch]&nbsp; &nbsp;<<< UPDATE i&nbsp; &nbsp; 01001402&nbsp; add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ecx,1&nbsp; &nbsp; 01001405&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],ecx&nbsp; &nbsp; 01001408&nbsp; jmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wmain+51h (10013F1h)&nbsp; &nbsp; &nbsp; <<< UNCOND JUMP&nbsp; }//“ DO-WHILE”循环:i = 0;.&nbsp; 0100140A&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],0&nbsp; do&nbsp; {&nbsp; &nbsp; &nbsp; x[i++] = 0;&nbsp; &nbsp; 01001411&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eax,dword ptr [ebp-0Ch]&nbsp; &nbsp;<<< DO THE JOB..&nbsp; &nbsp; 01001414&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte ptr [ebp+eax-78h],0&nbsp; &nbsp; 01001419&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ecx,dword ptr [ebp-0Ch]&nbsp; &nbsp;<<< UPDATE i&nbsp; &nbsp; 0100141C&nbsp; add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ecx,1&nbsp; &nbsp; 0100141F&nbsp; mov&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],ecx&nbsp; &nbsp; 01001422&nbsp; cmp&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dword ptr [ebp-0Ch],64h&nbsp; &nbsp;<<< TEST&nbsp; &nbsp; 01001426&nbsp; jl&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wmain+71h (1001411h)&nbsp; &nbsp; &nbsp; <<< COND JUMP&nbsp; }&nbsp; while (i<100);
打开App,查看更多内容
随时随地看视频慕课网APP