Learn how to use the for loop with the increment operator (++) to display numbers from 1 to 10.
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.
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:
int i = 1;)i <= 10;)i++)
The increment operator (++) increases the value of a variable by 1. For example, i++ is the same as i = i + 1.
++ operator is called the increment operator. It adds 1 to the variable. i++ is the same as i = i + 1.
i as the loop counter.i = 1 — starts from 1i <= 10 — runs until i is 10i++ — increases i by 1 each timeRun the program:
Sample Output:
i <= 10 to i <= 20 to display numbers from 1 to 20.i++ to i += 2 to display odd numbers (1, 3, 5, 7, 9).i = 1 to i = 5 to start from 5.| 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 |
| 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) |
for loop is a powerful and concise way to repeat a block of code a specific number of times.++ increases the value of the variable by 1.i = 1 to i = 10 (10 iterations).i changes from 1 to 10, and each value is printed.
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.
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.
for loop to count forward, backward, or with any step size by changing the initialization, condition, and increment expression.
ایک C/C++ پروگرام لکھنا جو for لوپ اور انکریمنٹ آپریٹر (++) کا استعمال کرتے ہوئے 1 سے 10 تک کے اعداد ظاہر کرے۔
for لوپ C/C++ میں سب سے زیادہ استعمال ہونے والے لوپنگ ڈھانچوں میں سے ایک ہے۔ یہ اس وقت استعمال ہوتا ہے جب آپ کو معلوم ہو کہ آپ کوڈ کے کسی بلاک کو کتنی بار دہرانا ہے۔ for لوپ کے تین حصے ہیں:
int i = 1;)i <= 10;)i++)
انکریمنٹ آپریٹر (++) متغیر کی قیمت کو 1 بڑھاتا ہے۔ مثال کے طور پر، i++، i = i + 1 کے برابر ہے۔
++ آپریٹر کو انکریمنٹ آپریٹر کہتے ہیں۔ یہ متغیر میں 1 کا اضافہ کرتا ہے۔ i++، i = i + 1 کے برابر ہے۔
i کا اعلان کرتا ہے۔i = 1 — 1 سے شروع کرتا ہےi <= 10 — جب تک i 10 ہے چلتا ہےi++ — ہر بار i کو 1 بڑھاتا ہےپروگرام چلائیں:
نمونہ آؤٹ پٹ:
| حصہ | مثال | مقصد |
|---|---|---|
| ابتدائیہ | 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 لوپ کو آگے، پیچھے، یا کسی بھی قدم کے سائز کے ساتھ شمار کرنے کے لیے استعمال کر سکتے ہیں۔