EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that prints the first 10 natural numbers (1 to 10) using the do-while loop.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of do-while loops

📖 Introduction

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:

do { // code to be repeated } while (condition);

In this practical, you will use a do-while loop to print numbers from 1 to 10.

💡 Tip: The do-while loop is useful when you want to execute the loop body at least once, regardless of the condition. This is especially useful for menu-driven programs.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named dowhile_1to10.c.
  3. Write the following code:
/* Practical 46: Program to print first 10 natural numbers using do-while loop */ #include <stdio.h> int main() { int i = 1; // Initialize counter /* ============================================ Using do-while loop to print numbers 1 to 10 ============================================ */ printf("========== FIRST 10 NATURAL NUMBERS ==========\n"); do { printf("%d\n", i); i++; // Increment the counter } while (i <= 10); printf("==============================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int i = 1; — Declares and initializes the counter variable to 1.
  3. do — Starts the do-while loop.
  4. printf("%d\n", i); — Prints the current value of i.
  5. i++; — Increments the counter by 1.
  6. while (i <= 10); — Checks if the counter is less than or equal to 10. If true, the loop repeats.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc dowhile_1to10.c -o dowhile_1to10

Run the program:

./dowhile_1to10

Sample Output:

========== FIRST 10 NATURAL NUMBERS ========== 1 2 3 4 5 6 7 8 9 10 ==============================================

Step 4: Modify the Program

  1. Print in one line: Change printf("%d\n", i); to printf("%d ", i);
  2. Print in reverse: Change i = 1 to i = 10 and i++ to i-- with condition i >= 1.
  3. Change the range: Change i <= 10 to i <= 20 to print 20 numbers.
  4. Take user input: Ask the user for the limit.
/* Modified: Print in one line with commas */ do { if (i == 10) printf("%d", i); else printf("%d, ", i); i++; } while (i <= 10);

📊 Do-While Loop Execution (Step-by-Step)

Iteration i Value Print 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)

📊 do-while vs while Loop Comparison

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

📊 Alternative Methods

/* Alternative 1: Using while loop */ int i = 1; while (i <= 10) { printf("%d\n", i); i++; }
/* Alternative 2: Using for loop */ for (int i = 1; i <= 10; i++) { printf("%d\n", i); }
/* Alternative 3: Print in reverse using do-while */ int i = 10; do { printf("%d\n", i); i--; } while (i >= 1);

📊 Observations

  • The do-while loop prints numbers from 1 to 10.
  • The loop body is executed at least once because the condition is checked after the body.
  • The counter starts at 1 and is incremented by 1 each time.
  • The loop runs exactly 10 times (from 1 to 10).
  • The do-while loop is useful when you need to execute the code at least once.

✅ Result

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.

📌 Important: The 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.
💡 Tip: Remember to include the semicolon after the while (condition); in the do-while loop. Forgetting it will cause a syntax error.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو پہلے 10 قدرتی اعداد (1 سے 10) کو do-while لوپ کا استعمال کرتے ہوئے پرنٹ کرے۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • do-while لوپ کی معلومات

📖 تعارف

do-while لوپ C/C++ میں ایک پوسٹ ٹیسٹ لوپ ہے۔ یہ لوپ باڈی کو کم از کم ایک بار شرط کو چیک کرنے سے پہلے انجام دیتا ہے۔ یہ while لوپ سے مختلف ہے جو پہلے شرط کو چیک کرتا ہے۔

do-while لوپ کا نحو یہ ہے:

do { // code to be repeated } while (condition);

اس عملی میں، آپ do-while لوپ کا استعمال کرتے ہوئے 1 سے 10 تک کے اعداد پرنٹ کریں گے۔

💡 مشورہ: do-while لوپ اس وقت مفید ہے جب آپ چاہتے ہیں کہ لوپ باڈی کم از کم ایک بار انجام پائے، چاہے شرط کچھ بھی ہو۔ یہ خاص طور پر مینو پر مبنی پروگراموں کے لیے مفید ہے۔

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. dowhile_1to10.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 46: Program to print first 10 natural numbers using do-while loop */ #include <stdio.h> int main() { int i = 1; // Initialize counter /* ============================================ Using do-while loop to print numbers 1 to 10 ============================================ */ printf("========== FIRST 10 NATURAL NUMBERS ==========\n"); do { printf("%d\n", i); i++; // Increment the counter } while (i <= 10); printf("==============================================\n"); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int i = 1; — کاؤنٹر متغیر کا اعلان اور 1 سے شروع کرتا ہے۔
  3. do — do-while لوپ شروع کرتا ہے۔
  4. printf("%d\n", i); — i کی موجودہ قیمت پرنٹ کرتا ہے۔
  5. i++; — کاؤنٹر کو 1 بڑھاتا ہے۔
  6. while (i <= 10); — چیک کرتا ہے کہ آیا کاؤنٹر 10 سے کم یا برابر ہے۔ اگر درست ہے تو لوپ دہراتا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S)۔
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc dowhile_1to10.c -o dowhile_1to10

پروگرام چلائیں:

./dowhile_1to10

نمونہ آؤٹ پٹ:

========== FIRST 10 NATURAL NUMBERS ========== 1 2 3 4 5 6 7 8 9 10 ==============================================

📊 do-while لوپ کا مرحلہ وار عمل

تکرار 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 ❌ غلط نہیں (لوپ ختم)

📊 do-while بمقابلہ while لوپ موازنہ

خصوصیت while لوپ do-while لوپ
شرط کی جانچ لوپ باڈی سے پہلے لوپ باڈی کے بعد
کم سے کم تکرار 0 (شاید انجام نہ پائے) 1 (کم از کم ایک بار انجام پاتا ہے)
نحو while (cond) { } do { } while (cond);
سیمی کولن } کے بعد ضرورت نہیں while (cond); کے بعد ضروری ہے
بہترین استعمال جب شرط پہلے چیک کرنی ہو جب لوپ باڈی کم از کم ایک بار چلنی چاہیے

📊 مشاہدات

  • do-while لوپ 1 سے 10 تک کے اعداد پرنٹ کرتا ہے۔
  • لوپ باڈی کم از کم ایک بار انجام پاتی ہے کیونکہ شرط باڈی کے بعد چیک ہوتی ہے۔
  • کاؤنٹر 1 سے شروع ہوتا ہے اور ہر بار 1 بڑھتا ہے۔
  • لوپ بالکل 10 بار چلتا ہے (1 سے 10 تک)۔
  • do-while لوپ اس وقت مفید ہے جب آپ کو کوڈ کو کم از کم ایک بار انجام دینے کی ضرورت ہو۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو do-while لوپ کا استعمال کرتے ہوئے پہلے 10 قدرتی اعداد پرنٹ کرتا ہے۔ آپ نے سیکھ لیا ہے کہ do-while لوپ while لوپ سے کیسے مختلف ہے اور اسے کب استعمال کرنا چاہیے۔

📌 اہم: do-while لوپ ہمیشہ لوپ باڈی کو کم از کم ایک بار انجام دیتا ہے، چاہے شرط غلط ہو۔ یہ مینو پر مبنی پروگراموں کے لیے مثالی ہے جہاں آپ مینو کو کم از کم ایک بار دکھانا چاہتے ہیں۔
💡 مشورہ: do-while لوپ میں while (condition); کے بعد سیمی کولن شامل کرنا یاد رکھیں۔ اسے بھولنے سے نحوی غلطی ہوگی۔
⬅ Back to Practical Notebooks