4.2.3 For loop

4.2.3 লুপৰ বাবে

In addition to the above-mentioned loops (while and do-while
), we use snother loop in C programing language-called for loop. A for loop has three portions.

ওপৰোক্ত লুপবোৰৰ উপৰিও (যেতিয়া আৰু কৰোঁতে)।
), আমি লুপৰ বাবে কোৱা ভাষা-কল কৰা চি প্ৰগ্ৰামিংত স্নোথাৰ লুপ ব্যৱহাৰ কৰোঁ। এটাফৰ লুপৰ তিনিটা অংশ আছে।

  1. Initialization expression আৰম্ভণিৰ অভিব্যক্তি
  2. Condition checking or testing expression অৱস্থা পৰীক্ষা বা পৰীক্ষাৰ অভিব্যক্তি
  3. Update expression অভিব্যক্তি আপডেট কৰক

The signature of the for loop is shown in Figure 4.4. we put a semicolon (;) to denote the end of an expression. The initialization expression contains all the statemente we want to execute before entering this generally contains intialization statements. The second expression contains all statements that need to be executed in each iteration of the loop.

লুপৰ বাবে স্বাক্ষৰ চিত্ৰ 4.4-ত দেখুওৱা হৈছে। আমি এটা ছেমিকলন ৰাখিছো (;) অভিব্যক্তি এটাৰ সমাপ্তি বুজাবলৈ। প্ৰাৰম্ভিককৰণ অভিব্যক্তিত ইয়াত প্ৰৱেশ কৰাৰ আগতে আমি কাৰ্যকৰী কৰিব বিচৰা সকলো বিবৃতি থাকে য’ত সাধাৰণতে অন্তৰ্ভুক্ত বিবৃতি থাকে। দ্বিতীয় অভিব্যক্তিটোত সকলো বিবৃতি থাকে যিবোৰ লুপৰ প্ৰতিটো পুনৰাবৃত্তিত কাৰ্যকৰী কৰিব লাগিব।

Figure 4.4: Signature of a for loop

We will now write the program of Example 4.2 using a for loop. If we recall Example 4.2, we initialized in the variable i to zero before the start of the loop in line number 4. We need to write this statement in the initialization expression of the for loop.

আমি এতিয়া লুপৰ বাবে এটা ব্যৱহাৰ কৰি উদাহৰণ 4.2 ৰ প্ৰ’গ্ৰাম লিখিম। যদি আমি উদাহৰণ 4.2 মনত পেলাওঁ, আমি শাৰী 4-ত লুপ আৰম্ভ হোৱাৰ আগতে চলক আইৰ পৰা শূন্যলৈ আৰম্ভ কৰিছিলোঁ। আমি এই বিবৃতিটো লুপৰ আৰম্ভণিঅভিব্যক্তিত লিখিব লাগিব।

Next we put a condition in the while loop whether the value of the variable i is less than 5 or not. we now need to write this statement in the condition checking expression of the for loop. In each iteration of the write loop, we have updated the variable i by 1. We will do the some thing in the update expression. Refer to the line number 5 of Example 4.8. The enclosed statement of the loop is a printf() statement and this is some in both the programs.

তাৰ পিছত আমি চলকৰ মূল্য 5-তকৈ কম নে নহয় সেই টোপত এটা চৰ্ত ৰাখিম। আমি এতিয়া এই বিবৃতিটো লুপৰ অভিব্যক্তি পৰীক্ষা কৰা অৱস্থাত লিখিব লাগিব। লিখা লুপৰ প্ৰতিটো পুনৰাবৃত্তিত, আমি চলক আই 1 ৰ দ্বাৰা আপডেট কৰিছোঁ। আমি আপডেট অভিব্যক্তিত কিছু কাম কৰিম। উদাহৰণ 4.8-ৰ শাৰী নম্বৰ 5 চাওক। লুপৰ আৱদ্ধ বিবৃতিটো হৈছে এক প্ৰিণ্টফ() বিবৃতি আৰু এইটো দুয়োটা প্ৰ’গ্ৰামৰ কিছুমান।

Example 4,8: AC program to print ”I rea in class X under SEBA”5 times using a for loop.

Let us now write a C program using a for loop to evaluate the following expression.

এতিয়া আমি নিম্নলিখিত অভিব্যক্তিটো মূল্যাঙ্কন কৰিবলৈ লুপৰ বাবে এটা ব্যৱহাৰ কৰি এটা চি প্ৰ’গ্ৰাম লিখোঁ আহক।

sum = 1+2+3+4+ …. + N

যোগফল = 1+2+3+4+ …+ এন

Here, N is an input from the user.

ইয়াত, এন হৈছে ব্যৱহাৰকাৰীৰ পৰা এটা ইনপুট।

The program is shown using Example 4.9. We get the value for N using a scanf() function in line number 6. In order to find the summation. We are using a for loop that runs for i 1 to N t This stop is similar to the program of Example 4.7.

প্ৰ’গ্ৰামটো উদাহৰণ 4.9 ব্যৱহাৰ কৰি দেখুওৱা হৈছে। আমি 6 নম্বৰ শাৰীত স্কেনফ() ফাংচন এটা ব্যৱহাৰ কৰি এন-ৰ মূল্য পাওঁ। সংক্ষিপ্তবিৱৰণ বিচাৰিবলৈ। আমি এটা লুপৰ বাবে ব্যৱহাৰ কৰি আছোঁ যি আই 1 ৰ পৰা এন টিলৈ চলে এই ষ্টপটো উদাহৰণ 4.7 ৰ প্ৰ’গ্ৰামৰ চিমিয়াৰ।

As the value of the variable i varies from 1 to N., We et the summation of its values as 1+2+3+4+ . . . + N at the end of the loop. The loop runs s long as the value of i less or equal to N. The ends when i becomes N+1.

যিহেতু চলকৰ মূল্য 1 ৰ পৰা এনলৈ পৃথক হয়, আমি ইয়াৰ মূল্যবোৰৰ সংক্ষিপ্তবিৱৰণ 1+2+3+4+ হিচাপে কৰো । . . + লুপৰ শেষত। লুপটো আইৰ মূল্য কম বা এনৰ সমান হোৱালৈকে দীঘলীয়া হয়। যেতিয়া মই এন+1 হওঁ তেতিয়া সমাপ্তি হয়।

Example 4.9 AC program to print the summation of a series 1+2+3+4+ . . . + N

Now we can extend this program to find the summation of N arbitrary numbers entered using the keyboard. The purpose of the for loop in the above program was to run the loop N times. In this program as well, we need a loop that runs N runes. Thus we will not change the for loop statement.

এতিয়া আমি কীবোৰ্ড ব্যৱহাৰ কৰি প্ৰবিষ্ট কৰা এন স্বেচ্ছাচাৰী নম্বৰৰ সংক্ষিপ্তবিৱৰণ বিচাৰিবলৈ এই প্ৰ’গ্ৰামটো সম্প্ৰসাৰিত কৰিব পাৰোঁ। ওপৰোক্ত প্ৰ’গ্ৰামত লুপৰ উদ্দেশ্য আছিল লুপ এন সময় চলোৱা। এই প্ৰ’গ্ৰামটোত, আমাক এটা লুপৰ প্ৰয়োজন যি এন ৰুন চলে। এনেদৰে আমি লুপ বিবৃতিৰ বাবে সলনি নকৰোঁ।

We need to change the statements that are written inside the loop. As we need to get a number from the at each iteration of the loop, We need to use a scanf() Function. We use a variable ”var”to read a number from the keyboard (line number 9 of Example 4.10). Then we add this number with the previously calculated summation to get the new summation (line number 12 of Example 4.10).

আমি লুপৰ ভিতৰত লিখা বিবৃতিবোৰ সলনি কৰিব লাগিব। যিহেতু আমি লুপৰ প্ৰতিটো পুনৰাবৃত্তিত এটা নম্বৰ প্ৰাপ্ত কৰিব লাগিব, আমি এটা স্কেনফ() ফাংচন ব্যৱহাৰ কৰিব লাগিব। আমি কীবোৰ্ডৰ পৰা এটা সংখ্যা পঢ়িবলৈ এটা চলক ”ভাৰ” ব্যৱহাৰ কৰোঁ (উদাহৰণ 4.10-ৰ শাৰী নম্বৰ 9)। তাৰ পিছত আমি নতুন সংক্ষিপ্তকৰণ প্ৰাপ্ত কৰিবলৈ পূৰ্বগণনা কৰা সংক্ষিপ্তকৰণৰ সৈতে এই সংখ্যাটো যোগ দিওঁ (উদাহৰণ 4.10-ৰ শাৰী নম্বৰ 12)।

Example 4.10: AC program to print the summation of N numbers entered using the keyboard.

IN all the programs written using a for loop, we have used only one statement in each expression. We may put more then one statement in an expression. This beyond the scope of the book and hence we will not write any programs using that feature.

লুপৰ বাবে ব্যৱহাৰ কৰি লিখা সকলো প্ৰ’গ্ৰামত, আমি প্ৰতিটো অভিব্যক্তিত কেৱল এটা বিবৃতি ব্যৱহাৰ কৰিছোঁ। আমি তাৰ পিছত এটা বিবৃতি এটা অভিব্যক্তিত ৰাখিব পাৰোঁ। এইটো কিতাপখনৰ পৰিসৰৰ বাহিৰত আৰু সেয়েহে আমি সেই সুবিধাটো ব্যৱহাৰ কৰি কোনো প্ৰ’গ্ৰাম নলিখিম।