0

重複の可能性:
「for(;;)」は「while(TRUE)」よりも高速ですか?そうでなければ、なぜ人々はそれを使うのですか?

どの無限ループがより好ましいか知りたいです:

for(;;) {
    // Do some stuff
}

また

#define TRUE 1

while(TRUE) {
    // Do some stuff
}

パフォーマンスの観点から、それらの間に違いはありますか?

コーディング標準の観点からより好ましいものは何ですか?

4

5 に答える 5

4

From a performance point of view it doesn't matter. The compiler will optimize both into the same assembly code. From a "coding standards" point of view, for(;;) is more terse, which is nice. Although while(TRUE) is a bit clearer, for(;;) is so common that clarity in this case is not much of an issue (in fact, the ubiquity of for(;;) may make it more clear than the while version).

于 2012-11-01T11:18:05.477 に答える
1

変わりはない。while (true)一部の人にはもっと明白に見えるかもしれません。

于 2012-11-01T11:16:30.863 に答える
1
于 2012-11-01T11:16:47.327 に答える
1

It's matter of taste, but especially matter of coding conventions used. Preferably one style should be used everywhere in same project, or same rules used in picking the style when they will be mixes. Many C++ frameworks also provide forever as macro, which in my opinion should then be preferred in code using the framework. There should be no difference in generated code.

于 2012-11-01T11:19:45.117 に答える
-1

The difference between the two is not big. You usually use for when you know how many elements to traverse. You use while if instead a condition needs to be satisfied

于 2012-11-01T11:17:18.940 に答える