cpp / beginner
Snippet
Do-While Schleife: Garantierte erste Ausführung
Die do-while Schleife führt ihren Körper immer mindestens einmal aus, bevor sie die Bedingung prüft. Im Gegensatz zur normalen while-Schleife, die zuerst prüft, garantiert do-while, dass der Code einmal ausgeführt wird, unabhängig davon, ob die Bedingung anfangs wahr oder falsch ist.
snippet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>using namespace std;int main() {int count = 0;do {cout << "Count: " << count << endl;count++;} while (count < 3);return 0;}
Erklärung
1
do { ... } while (condition);
Körper wird zuerst ausgeführt, dann wird die Bedingung geprüft; Semikolon nach while erforderlich