Do While Loop
Do While Loop
execute at least once, regardless of the condition. This is because the condition is checked after
the loop body has been executed.
Syntax:
do {
// Code to be executed at least once
} while (condition);
Explanation:
Example:
#include <iostream>
using namespace std;
int main() {
int count = 1;
return 0;
}
Output:
csharp
Copy code
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Key Differences Between while and do-while:
1. Execution Guarantee:
o while: The loop body might not execute if the condition is false initially.
o do-while: The loop body executes at least once, even if the condition is false
initially.
2. Condition Checking:
o while: Checks the condition before executing the loop body.
o do-while: Checks the condition after executing the loop body.