🎯 Aim
To write a C/C++ program that displays the word "PAKISTAN" five times using the while loop.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of while loops
📖 Introduction
The while loop is a fundamental looping structure in C/C++. It repeats a block of code as long as a given condition is true.
The syntax of the while loop is:
while (condition)
{
// code to be repeated
}
In this practical, you will use a while loop to print the word "PAKISTAN" five times. This is a simple but important example of how loops work.
💡 Tip: The while loop is useful when you don't know how many times you need to repeat a block of code, but in this case, we know we want exactly 5 iterations.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named pakistan_while.c.
- Write the following code:
#include <stdio.h>
int main()
{
int count = 1;
printf("========== PAKISTAN ==========\n");
while (count <= 5)
{
printf("%d. PAKISTAN\n", count);
count++;
}
printf("===============================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int count = 1; — Declares and initializes a counter variable to 1.
- while (count <= 5) — The while loop runs as long as
count is less than or equal to 5.
- printf("%d. PAKISTAN\n", count); — Displays the word "PAKISTAN" with a number.
- count++; — Increments the counter by 1 after each iteration.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc pakistan_while.c -o pakistan_while
./pakistan_while
Sample Output:
========== PAKISTAN ==========
1. PAKISTAN
2. PAKISTAN
3. PAKISTAN
4. PAKISTAN
5. PAKISTAN
===============================
Step 4: Modify the Program
- Change the number of times: Change
count <= 5 to count <= 10 to display 10 times.
- Display in a different format: Remove the number and just display "PAKISTAN" without numbering.
- Take user input: Ask the user how many times they want to display "PAKISTAN".
int num, count = 1;
printf("How many times to display PAKISTAN? ");
scanf("%d", &num);
while (count <= num)
{
printf("PAKISTAN\n");
count++;
}
📊 While Loop Execution (Step-by-Step)
| Iteration |
count Value |
Condition count <= 5 |
Output |
count++ |
| 1 |
1 |
✅ True |
1. PAKISTAN |
2 |
| 2 |
2 |
✅ True |
2. PAKISTAN |
3 |
| 3 |
3 |
✅ True |
3. PAKISTAN |
4 |
| 4 |
4 |
✅ True |
4. PAKISTAN |
5 |
| 5 |
5 |
✅ True |
5. PAKISTAN |
6 |
| 6 |
6 |
❌ False |
(Loop ends) |
- |
📊 While Loop Syntax
| Part |
Example |
Purpose |
| Initialization |
int count = 1; |
Sets the starting value |
| Condition |
count <= 5 |
Checks if the loop should continue |
| Loop Body |
printf("PAKISTAN\n"); |
Code that runs in each iteration |
| Update |
count++; |
Changes the counter to avoid infinite loop |
📊 Alternative Methods
for (int i = 1; i <= 5; i++)
{
printf("%d. PAKISTAN\n", i);
}
int count = 1;
do
{
printf("%d. PAKISTAN\n", count);
count++;
} while (count <= 5);
int count = 5;
while (count >= 1)
{
printf("%d. PAKISTAN\n", count);
count--;
}
📊 Observations
- The
while loop repeatedly executes the code inside its body as long as the condition is true.
- The counter variable
count starts at 1 and goes up to 5.
- Each iteration prints the word "PAKISTAN" with a number.
- After printing 5 times, the condition becomes false and the loop ends.
- You can change the condition to print more or fewer times.
✅ Result
You have successfully written a C program that displays the word "PAKISTAN" five times using the while loop. You have learned how to use the while loop structure for repetition and how to manage a counter variable.
📌 Important: Make sure to update the counter variable inside the loop. If you forget to increment the counter, the loop will run forever (infinite loop).
💡 Tip: The while loop is perfect when you need to repeat a block of code until a certain condition is met. You can also use it to read data until the end of a file or until the user enters a specific value.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو "PAKISTAN" کو پانچ بار while لوپ کا استعمال کرتے ہوئے ظاہر کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- while لوپ کی معلومات
📖 تعارف
while لوپ C/C++ میں ایک بنیادی لوپنگ ڈھانچہ ہے۔ یہ کوڈ کے کسی بلاک کو اس وقت تک دہراتا ہے جب تک کہ دی گئی شرط درست ہے۔
while لوپ کا نحو یہ ہے:
while (condition)
{
// code to be repeated
}
اس عملی میں، آپ while لوپ کا استعمال کرتے ہوئے "PAKISTAN" کو پانچ بار پرنٹ کریں گے۔ یہ ایک سادہ مگر اہم مثال ہے جو لوپ کے کام کو ظاہر کرتی ہے۔
💡 مشورہ: while لوپ اس وقت مفید ہے جب آپ کو معلوم نہ ہو کہ آپ کو کوڈ کے کسی بلاک کو کتنی بار دہرانا ہے، لیکن اس صورت میں، ہم جانتے ہیں کہ ہمیں بالکل 5 تکرار چاہیے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- pakistan_while.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int count = 1;
printf("========== PAKISTAN ==========\n");
while (count <= 5)
{
printf("%d. PAKISTAN\n", count);
count++;
}
printf("===============================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int count = 1; — ایک کاؤنٹر متغیر کا اعلان اور 1 سے شروع کرتا ہے۔
- while (count <= 5) — while لوپ اس وقت تک چلتا ہے جب تک
count 5 سے کم یا برابر ہے۔
- printf("%d. PAKISTAN\n", count); — "PAKISTAN" کو نمبر کے ساتھ ظاہر کرتا ہے۔
- count++; — ہر تکرار کے بعد کاؤنٹر کو 1 بڑھاتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc pakistan_while.c -o pakistan_while
./pakistan_while
نمونہ آؤٹ پٹ:
========== PAKISTAN ==========
1. PAKISTAN
2. PAKISTAN
3. PAKISTAN
4. PAKISTAN
5. PAKISTAN
===============================
📊 while لوپ کا مرحلہ وار عمل
| تکرار |
count کی قیمت |
شرط count <= 5 |
آؤٹ پٹ |
count++ |
| 1 |
1 |
✅ درست |
1. PAKISTAN |
2 |
| 2 |
2 |
✅ درست |
2. PAKISTAN |
3 |
| 3 |
3 |
✅ درست |
3. PAKISTAN |
4 |
| 4 |
4 |
✅ درست |
4. PAKISTAN |
5 |
| 5 |
5 |
✅ درست |
5. PAKISTAN |
6 |
| 6 |
6 |
❌ غلط |
(لوپ ختم) |
- |
📊 مشاہدات
while لوپ اس وقت تک اپنے باڈی کے اندر موجود کوڈ کو بار بار انجام دیتا ہے جب تک کہ شرط درست ہے۔
- کاؤنٹر متغیر
count 1 سے شروع ہوتا ہے اور 5 تک جاتا ہے۔
- ہر تکرار "PAKISTAN" کو نمبر کے ساتھ پرنٹ کرتی ہے۔
- 5 بار پرنٹ کرنے کے بعد، شرط غلط ہو جاتی ہے اور لوپ ختم ہو جاتا ہے۔
- آپ شرط کو تبدیل کر کے زیادہ یا کم بار پرنٹ کر سکتے ہیں۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ کا استعمال کرتے ہوئے "PAKISTAN" کو پانچ بار ظاہر کرتا ہے۔ آپ نے تکرار کے لیے while لوپ کے ڈھانچے اور کاؤنٹر متغیر کو منظم کرنا سیکھ لیا ہے۔
📌 اہم: یقینی بنائیں کہ آپ لوپ کے اندر کاؤنٹر متغیر کو اپ ڈیٹ کریں۔ اگر آپ کاؤنٹر کو بڑھانا بھول گئے تو لوپ ہمیشہ کے لیے چلتا رہے گا (لامحدود لوپ)۔
💡 مشورہ: while لوپ اس وقت بہترین ہے جب آپ کو کوڈ کے کسی بلاک کو اس وقت تک دہرانا ہو جب تک کہ کوئی خاص شرط پوری نہ ہو جائے۔ آپ اسے ڈیٹا پڑھنے کے لیے بھی استعمال کر سکتے ہیں جب تک کہ فائل کا خاتمہ نہ ہو جائے یا جب تک صارف کوئی خاص قدر درج نہ کرے۔