EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that uses the for loop with the increment operator (++) to display numbers from 1 to 10 on the screen.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of for loops and increment operator

📖 Introduction

The for loop is one of the most commonly used looping structures in C/C++. It is used when you know exactly how many times you want to repeat a block of code. The for loop has three parts:

  • Initialization: Sets the starting value (e.g., int i = 1;)
  • Condition: Checks if the loop should continue (e.g., i <= 10;)
  • Increment/Decrement: Updates the value after each iteration (e.g., i++)

The increment operator (++) increases the value of a variable by 1. For example, i++ is the same as i = i + 1.

💡 Tip: The ++ operator is called the increment operator. It adds 1 to the variable. i++ is the same as i = i + 1.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named for_loop_1to10.c.
  3. Write the following code:
/* Practical 33: Program to display numbers from 1 to 10 using for loop */ #include <stdio.h> int main() { int i; /* ============================================ Using for loop with increment operator (++) to display numbers from 1 to 10 ============================================ */ printf("========== NUMBERS 1 to 10 ==========\n"); for (i = 1; i <= 10; i++) { printf("%d\n", i); } printf("======================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int i; — Declares an integer variable i as the loop counter.
  3. for (i = 1; i <= 10; i++) — The for loop with three parts:
    • Initialization: i = 1 — starts from 1
    • Condition: i <= 10 — runs until i is 10
    • Increment: i++ — increases i by 1 each time
  4. printf("%d\n", i); — Displays the current value of i.

Step 3: Save, Compile, and Run

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

Run the program:

./for_loop_1to10

Sample Output:

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

Step 4: Modify the Program

  1. Change the range: Change i <= 10 to i <= 20 to display numbers from 1 to 20.
  2. Change the increment: Change i++ to i += 2 to display odd numbers (1, 3, 5, 7, 9).
  3. Change the starting value: Change i = 1 to i = 5 to start from 5.
/* Modified: Display odd numbers from 1 to 10 */ for (i = 1; i <= 10; i = i + 2) { printf("%d\n", i); }

📊 For Loop Syntax

Part Example Purpose
Initialization i = 1 Sets the starting value of the loop variable
Condition i <= 10 Checks if the loop should continue
Increment i++ Updates the loop variable after each iteration
Body printf("%d\n", i); Code that runs in each iteration

📊 Different Increment Methods

Method Example Result
i++ i = 1; i++ i becomes 2 (increment by 1)
++i i = 1; ++i i becomes 2 (pre-increment)
i += 1 i = 1; i += 1 i becomes 2 (increment by 1)
i = i + 1 i = 1; i = i + 1 i becomes 2 (increment by 1)
i += 2 i = 1; i += 2 i becomes 3 (increment by 2)

📊 Alternative Methods

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

📊 Observations

  • The for loop is a powerful and concise way to repeat a block of code a specific number of times.
  • The increment operator ++ increases the value of the variable by 1.
  • The loop runs from i = 1 to i = 10 (10 iterations).
  • The variable i changes from 1 to 10, and each value is printed.
  • You can change the starting value, ending condition, and increment to customize the loop.

✅ Result

You have successfully written a C program that uses the for loop with the increment operator (++) to display numbers from 1 to 10. You have learned how to use the for loop structure and the increment operator for iteration.

📌 Important: The for loop is ideal when you know exactly how many times you want to repeat a block of code. The increment operator ++ is commonly used to advance the loop counter.
💡 Tip: You can use the for loop to count forward, backward, or with any step size by changing the initialization, condition, and increment expression.

🎯 مقصد

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

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • for لوپ اور انکریمنٹ آپریٹر کی معلومات

📖 تعارف

for لوپ C/C++ میں سب سے زیادہ استعمال ہونے والے لوپنگ ڈھانچوں میں سے ایک ہے۔ یہ اس وقت استعمال ہوتا ہے جب آپ کو معلوم ہو کہ آپ کوڈ کے کسی بلاک کو کتنی بار دہرانا ہے۔ for لوپ کے تین حصے ہیں:

  • ابتدائیہ (Initialization): ابتدائی قیمت سیٹ کرتا ہے (مثال: int i = 1;)
  • شرط (Condition): چیک کرتا ہے کہ آیا لوپ جاری رہنا چاہیے (مثال: i <= 10;)
  • انکریمنٹ/ڈیکریمنٹ: ہر تکرار کے بعد قیمت کو اپ ڈیٹ کرتا ہے (مثال: i++)

انکریمنٹ آپریٹر (++) متغیر کی قیمت کو 1 بڑھاتا ہے۔ مثال کے طور پر، i++، i = i + 1 کے برابر ہے۔

💡 مشورہ: ++ آپریٹر کو انکریمنٹ آپریٹر کہتے ہیں۔ یہ متغیر میں 1 کا اضافہ کرتا ہے۔ i++، i = i + 1 کے برابر ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. for_loop_1to10.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 33: Program to display numbers from 1 to 10 using for loop */ #include <stdio.h> int main() { int i; /* ============================================ Using for loop with increment operator (++) to display numbers from 1 to 10 ============================================ */ printf("========== NUMBERS 1 to 10 ==========\n"); for (i = 1; i <= 10; i++) { printf("%d\n", i); } printf("======================================\n"); return 0; }

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

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

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

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

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

./for_loop_1to10

نمونہ آؤٹ پٹ:

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

📊 for لوپ کا نحو

حصہ مثال مقصد
ابتدائیہ i = 1 لوپ متغیر کی ابتدائی قیمت سیٹ کرتا ہے
شرط i <= 10 چیک کرتا ہے کہ آیا لوپ جاری رہنا چاہیے
انکریمنٹ i++ ہر تکرار کے بعد لوپ متغیر کو اپ ڈیٹ کرتا ہے
باڈی printf("%d\n", i); کوڈ جو ہر تکرار میں چلتا ہے

📊 مختلف انکریمنٹ طریقے

طریقہ مثال نتیجہ
i++ i = 1; i++ i 2 ہو جاتا ہے (1 کا اضافہ)
++i i = 1; ++i i 2 ہو جاتا ہے (پری-انکریمنٹ)
i += 1 i = 1; i += 1 i 2 ہو جاتا ہے (1 کا اضافہ)
i = i + 1 i = 1; i = i + 1 i 2 ہو جاتا ہے (1 کا اضافہ)
i += 2 i = 1; i += 2 i 3 ہو جاتا ہے (2 کا اضافہ)

📊 مشاہدات

  • for لوپ کوڈ کے کسی بلاک کو ایک مخصوص تعداد میں دہرانے کا ایک طاقتور اور مختصر طریقہ ہے۔
  • انکریمنٹ آپریٹر ++ متغیر کی قیمت کو 1 بڑھاتا ہے۔
  • لوپ i = 1 سے i = 10 تک چلتا ہے (10 تکرار)۔
  • متغیر i 1 سے 10 تک تبدیل ہوتا ہے، اور ہر قیمت پرنٹ ہوتی ہے۔
  • آپ لوپ کو حسب ضرورت بنانے کے لیے ابتدائی قیمت، اختتامی شرط اور انکریمنٹ کو تبدیل کر سکتے ہیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو for لوپ اور انکریمنٹ آپریٹر (++) کا استعمال کرتے ہوئے 1 سے 10 تک کے اعداد ظاہر کرتا ہے۔ آپ نے for لوپ کے ڈھانچے اور تکرار کے لیے انکریمنٹ آپریٹر کا استعمال سیکھ لیا ہے۔

📌 اہم: for لوپ اس وقت بہترین ہے جب آپ کو معلوم ہو کہ آپ کوڈ کے کسی بلاک کو کتنی بار دہرانا ہے۔ انکریمنٹ آپریٹر ++ عام طور پر لوپ کاؤنٹر کو آگے بڑھانے کے لیے استعمال ہوتا ہے۔
💡 مشورہ: آپ ابتدائیہ، شرط اور انکریمنٹ ایکسپریشن کو تبدیل کرکے for لوپ کو آگے، پیچھے، یا کسی بھی قدم کے سائز کے ساتھ شمار کرنے کے لیے استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks