Do While Schleife
-
Hallo.
Eine Frage zu einer Do While Schleife und welches der schönere Programmierstil ist.
Meine Do While Schleife hat 2 Bedingungen. Und ich sollte nun wissen welche Bedingung erfüllt bzw. nicht mehr erfüllt ist.
Möglichkeit 1: While(true) und mit 2 if Abfragen einen break auslösen.
Möglichkeit 2: Die beiden bedingungen in die While eintragen und danach nochmals abfragen welche Bedingung nun eingetretten ist.
-
Geschmacksache:
bool bCondition1, bCondition2; while ((bCondition1=CheckCondition1()) && (bCondition2=CheckCondition1())) DoSomethingAsLongAsBothAreTrue(); if (!bCondition1) Condition1Failed(); if (!bCondition2) Condition2Failed();
oder
for (;;) { if (!CheckCondition1()) { Condition1Failed(); break; } if (!CheckCondition2()) { Condition2Failed(); break; } DoSomethingAsLongAsBothAreTrue(); }
Mir gefällt persönlich die erste Variante besser.
-
Und so:
while(true) { DoSomethingAsLongAsBothAreTrue(); if( bCondition1 = CheckCondition1()) break; if( bCondition2 = CheckCondition2()) break; }
oder so
while (true) { if (!CheckCondition1()) { Condition1Failed(); break; } if (!CheckCondition2()) { Condition2Failed(); break; } DoSomethingAsLongAsBothAreTrue(); }
- Nicht erlaubt?
- Unschön?
- Fehleranfällig?