EZ Notes Logo

EZNotes

🎯 Aim

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

🛠️ What You Need

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

📖 Introduction

The for loop is a powerful looping structure in C/C++. It can be used to count both upward and downward. In this practical, you will use the decrement operator (--) to count from 10 down to 1.

The decrement operator (--) decreases the value of a variable by 1. For example, i-- is the same as i = i - 1.

The for loop structure for counting backwards:

  • Initialization: int i = 10; (start from 10)
  • Condition: i >= 1; (stop when i is less than 1)
  • Decrement: i-- (decrease i by 1 each time)
💡 Tip: The -- operator is called the decrement operator. It subtracts 1 from 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_10to1.c.
  3. Write the following code:
/* Practical 34: Program to display numbers from 10 to 1 using for loop */ #include <stdio.h> int main() { int i; /* ============================================ Using for loop with decrement operator (--) to display numbers from 10 to 1 ============================================ */ printf("========== NUMBERS 10 to 1 ==========\n"); for (i = 10; i >= 1; 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 = 10; i >= 1; i--) — The for loop with three parts:
    • Initialization: i = 10 — starts from 10
    • Condition: i >= 1 — runs until i is 1
    • Decrement: i-- — decreases 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_10to1.c -o for_loop_10to1

Run the program:

./for_loop_10to1

Sample Output:

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

Step 4: Modify the Program

  1. Change the range: Change i >= 1 to i >= 0 to include 0.
  2. Change the decrement: Change i-- to i -= 2 to display even numbers (10, 8, 6, 4, 2).
  3. Change the starting value: Change i = 10 to i = 20 to start from 20.
/* Modified: Display even numbers from 10 to 2 */ for (i = 10; i >= 2; i -= 2) { printf("%d\n", i); }

📊 For Loop with Decrement - Syntax

Part Example Purpose
Initialization i = 10 Sets the starting value (highest)
Condition i >= 1 Checks if the loop should continue
Decrement i-- Decreases the loop variable by 1
Body printf("%d\n", i); Code that runs in each iteration

📊 Increment vs Decrement Comparison

Direction Operator Example Result
Forward (1 to 10) ++ (increment) for (i = 1; i <= 10; i++) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Backward (10 to 1) -- (decrement) for (i = 10; i >= 1; i--) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Forward by 2 += for (i = 1; i <= 10; i += 2) 1, 3, 5, 7, 9
Backward by 2 -= for (i = 10; i >= 1; i -= 2) 10, 8, 6, 4, 2

📊 Different Decrement Methods

Method Example Result
i-- i = 10; i-- i becomes 9 (decrement by 1)
--i i = 10; --i i becomes 9 (pre-decrement)
i -= 1 i = 10; i -= 1 i becomes 9 (decrement by 1)
i = i - 1 i = 10; i = i - 1 i becomes 9 (decrement by 1)
i -= 2 i = 10; i -= 2 i becomes 8 (decrement by 2)

📊 Alternative Methods

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

📊 Observations

  • The for loop can count backward using the decrement operator (--).
  • The loop runs from i = 10 down to i = 1 (10 iterations).
  • The variable i changes from 10 to 1, and each value is printed.
  • The decrement operator -- reduces the value of the variable by 1.
  • You can change the starting value, ending condition, and decrement to customize the loop.

✅ Result

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

📌 Important: The for loop can count in any direction by changing the initialization, condition, and increment/decrement expression. The decrement operator -- is used for counting backward.
💡 Tip: You can use the for loop to count backward with any step size by changing the decrement expression (e.g., i -= 2 for even numbers).

🎯 مقصد

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

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

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

📖 تعارف

for لوپ C/C++ میں ایک طاقتور لوپنگ ڈھانچہ ہے۔ اسے اوپر اور نیچے دونوں طرف شمار کرنے کے لیے استعمال کیا جا سکتا ہے۔ اس عملی میں، آپ ڈیکریمنٹ آپریٹر (--) کا استعمال کرتے ہوئے 10 سے 1 تک شمار کریں گے۔

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

پیچھے کی طرف شمار کرنے کے لیے for لوپ کا ڈھانچہ:

  • ابتدائیہ: int i = 10; (10 سے شروع کریں)
  • شرط: i >= 1; (جب i 1 سے کم ہو جائے تو رک جائے)
  • ڈیکریمنٹ: i-- (ہر بار i کو 1 کم کریں)
💡 مشورہ: -- آپریٹر کو ڈیکریمنٹ آپریٹر کہتے ہیں۔ یہ متغیر سے 1 کم کرتا ہے۔ i--، i = i - 1 کے برابر ہے۔

📝 اقدامات

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

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

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

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

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

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

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

./for_loop_10to1

نمونہ آؤٹ پٹ:

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

📊 ڈیکریمنٹ کے ساتھ for لوپ کا نحو

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

📊 انکریمنٹ بمقابلہ ڈیکریمنٹ موازنہ

سمت آپریٹر مثال نتیجہ
آگے (1 سے 10) ++ (انکریمنٹ) for (i = 1; i <= 10; i++) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
پیچھے (10 سے 1) -- (ڈیکریمنٹ) for (i = 10; i >= 1; i--) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
آگے 2 سے += for (i = 1; i <= 10; i += 2) 1, 3, 5, 7, 9
پیچھے 2 سے -= for (i = 10; i >= 1; i -= 2) 10, 8, 6, 4, 2

📊 مشاہدات

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

✅ نتیجہ

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

📌 اہم: for لوپ ابتدائیہ، شرط اور انکریمنٹ/ڈیکریمنٹ ایکسپریشن کو تبدیل کرکے کسی بھی سمت میں شمار کر سکتا ہے۔ ڈیکریمنٹ آپریٹر -- پیچھے کی طرف شمار کرنے کے لیے استعمال ہوتا ہے۔
💡 مشورہ: آپ ڈیکریمنٹ ایکسپریشن کو تبدیل کرکے (مثال: i -= 2) for لوپ کو کسی بھی قدم کے سائز کے ساتھ پیچھے کی طرف شمار کرنے کے لیے استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks