Learn how to use the for loop with the decrement operator (--) to display numbers from 10 down to 1.
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.
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:
int i = 10; (start from 10)i >= 1; (stop when i is less than 1)i-- (decrease i by 1 each time)-- operator is called the decrement operator. It subtracts 1 from the variable. i-- is the same as i = i - 1.
i as the loop counter.i = 10 — starts from 10i >= 1 — runs until i is 1i-- — decreases i by 1 each timeRun the program:
Sample Output:
i >= 1 to i >= 0 to include 0.i-- to i -= 2 to display even numbers (10, 8, 6, 4, 2).i = 10 to i = 20 to start from 20.| 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 |
| 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 |
| 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) |
for loop can count backward using the decrement operator (--).i = 10 down to i = 1 (10 iterations).i changes from 10 to 1, and each value is printed.-- reduces the value of the variable by 1.
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.
for loop can count in any direction by changing the initialization, condition, and increment/decrement expression. The decrement operator -- is used for counting backward.
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 تک کے اعداد ظاہر کرے۔
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 کے برابر ہے۔
i کا اعلان کرتا ہے۔i = 10 — 10 سے شروع کرتا ہےi >= 1 — جب تک i 1 ہے چلتا ہےi-- — ہر بار i کو 1 کم کرتا ہےپروگرام چلائیں:
نمونہ آؤٹ پٹ:
| حصہ | مثال | مقصد |
|---|---|---|
| ابتدائیہ | 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 لوپ کو کسی بھی قدم کے سائز کے ساتھ پیچھے کی طرف شمار کرنے کے لیے استعمال کر سکتے ہیں۔