Learn how to use the do-while loop to print numbers from 1 to 10.
To write a C/C++ program that prints the first 10 natural numbers (1 to 10) using the do-while loop.
The do-while loop is a post-test loop in C/C++. It executes the loop body at least once before checking the condition. This is different from the while loop, which checks the condition first.
The syntax of the do-while loop is:
In this practical, you will use a do-while loop to print numbers from 1 to 10.
i.Run the program:
Sample Output:
printf("%d\n", i); to printf("%d ", i);i = 1 to i = 10 and i++ to i-- with condition i >= 1.i <= 10 to i <= 20 to print 20 numbers.| Iteration | i Value |
i++ |
Condition i <= 10 |
Loop Continues? | |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 2 | ✅ True | Yes |
| 2 | 2 | 2 | 3 | ✅ True | Yes |
| 3 | 3 | 3 | 4 | ✅ True | Yes |
| 4 | 4 | 4 | 5 | ✅ True | Yes |
| 5 | 5 | 5 | 6 | ✅ True | Yes |
| 6 | 6 | 6 | 7 | ✅ True | Yes |
| 7 | 7 | 7 | 8 | ✅ True | Yes |
| 8 | 8 | 8 | 9 | ✅ True | Yes |
| 9 | 9 | 9 | 10 | ✅ True | Yes |
| 10 | 10 | 10 | 11 | ❌ False | No (Loop ends) |
| Feature | while Loop |
do-while Loop |
|---|---|---|
| Condition Check | Before loop body | After loop body |
| Minimum Executions | 0 (may not execute) | 1 (always executes at least once) |
| Syntax | while (cond) { } |
do { } while (cond); |
| Semicolon | Not required after } |
Required after while (cond); |
| Best Used For | When condition should be checked first | When loop body should run at least once |
do-while loop prints numbers from 1 to 10.do-while loop is useful when you need to execute the code at least once.You have successfully written a C program that prints the first 10 natural numbers using the do-while loop. You have learned how the do-while loop differs from the while loop and when to use it.
do-while loop always executes the loop body at least once, even if the condition is false. This makes it ideal for menu-driven programs where you want to display the menu at least once.
while (condition); in the do-while loop. Forgetting it will cause a syntax error.
ایک C/C++ پروگرام لکھنا جو پہلے 10 قدرتی اعداد (1 سے 10) کو do-while لوپ کا استعمال کرتے ہوئے پرنٹ کرے۔
do-while لوپ C/C++ میں ایک پوسٹ ٹیسٹ لوپ ہے۔ یہ لوپ باڈی کو کم از کم ایک بار شرط کو چیک کرنے سے پہلے انجام دیتا ہے۔ یہ while لوپ سے مختلف ہے جو پہلے شرط کو چیک کرتا ہے۔
do-while لوپ کا نحو یہ ہے:
اس عملی میں، آپ do-while لوپ کا استعمال کرتے ہوئے 1 سے 10 تک کے اعداد پرنٹ کریں گے۔
i کی موجودہ قیمت پرنٹ کرتا ہے۔پروگرام چلائیں:
نمونہ آؤٹ پٹ:
| تکرار | i کی قیمت |
پرنٹ | i++ |
شرط i <= 10 |
لوپ جاری رہتا ہے؟ |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 2 | ✅ درست | ہاں |
| 2 | 2 | 2 | 3 | ✅ درست | ہاں |
| 3 | 3 | 3 | 4 | ✅ درست | ہاں |
| 4 | 4 | 4 | 5 | ✅ درست | ہاں |
| 5 | 5 | 5 | 6 | ✅ درست | ہاں |
| 6 | 6 | 6 | 7 | ✅ درست | ہاں |
| 7 | 7 | 7 | 8 | ✅ درست | ہاں |
| 8 | 8 | 8 | 9 | ✅ درست | ہاں |
| 9 | 9 | 9 | 10 | ✅ درست | ہاں |
| 10 | 10 | 10 | 11 | ❌ غلط | نہیں (لوپ ختم) |
| خصوصیت | while لوپ |
do-while لوپ |
|---|---|---|
| شرط کی جانچ | لوپ باڈی سے پہلے | لوپ باڈی کے بعد |
| کم سے کم تکرار | 0 (شاید انجام نہ پائے) | 1 (کم از کم ایک بار انجام پاتا ہے) |
| نحو | while (cond) { } |
do { } while (cond); |
| سیمی کولن | } کے بعد ضرورت نہیں |
while (cond); کے بعد ضروری ہے |
| بہترین استعمال | جب شرط پہلے چیک کرنی ہو | جب لوپ باڈی کم از کم ایک بار چلنی چاہیے |
do-while لوپ 1 سے 10 تک کے اعداد پرنٹ کرتا ہے۔do-while لوپ اس وقت مفید ہے جب آپ کو کوڈ کو کم از کم ایک بار انجام دینے کی ضرورت ہو۔آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو do-while لوپ کا استعمال کرتے ہوئے پہلے 10 قدرتی اعداد پرنٹ کرتا ہے۔ آپ نے سیکھ لیا ہے کہ do-while لوپ while لوپ سے کیسے مختلف ہے اور اسے کب استعمال کرنا چاہیے۔
do-while لوپ ہمیشہ لوپ باڈی کو کم از کم ایک بار انجام دیتا ہے، چاہے شرط غلط ہو۔ یہ مینو پر مبنی پروگراموں کے لیے مثالی ہے جہاں آپ مینو کو کم از کم ایک بار دکھانا چاہتے ہیں۔
while (condition); کے بعد سیمی کولن شامل کرنا یاد رکھیں۔ اسے بھولنے سے نحوی غلطی ہوگی۔