0% found this document useful (0 votes)
9 views2 pages

Do While Loop

Loop

Uploaded by

amirkhattak3136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Do While Loop

Loop

Uploaded by

amirkhattak3136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

In C++, a do-while loop is similar to a while loop, but it guarantees that the loop body will

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:

 The body of the loop is executed first.


 The condition is evaluated after the execution of the loop body. If it's true, the loop will
run again; if it's false, the loop terminates.

Example:
#include <iostream>
using namespace std;

int main() {
int count = 1;

// Do-while loop will execute the body at least once


do {
cout << "Count is: " << count << endl;
count++; // Increment the count
} while (count <= 5);

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.

You might also like