A For Loop هو عبارة تكرارية تستخدم للتحقق من وجود بعض الشرط ، وبناءً على الشرط ، يتم تنفيذ كتلة من التعليمات البرمجية بشكل متكرر حتى يتم استيفاء الشرط المحدد.
يتم تمييز حلقة for عن حلقات التكرار الأخرى من خلال متغير حلقة صريح أو متغير حلقة يسمح للجسم من الحلقة معرفة التسلسل الدقيق لكل تكرار.
ومن ثم ، فإن حلقة for هي بنية تحكم في التكرار تسمح لك بكتابة حلقة بكفاءة تحتاج إلى تنفيذ عدد محدد من المرات.
تسمح لك هذه الخطوة بتعريف وتهيئة أي متغيرات تحكم في الحلقة. يتم تنفيذ هذه الخطوة أولاً ومرة واحدة فقط.
### شرط
القادم يتم تقييم الشرط. إذا كان صحيحًا ، فسيتم تنفيذ نص الحلقة. إذا كانت تحتوي على false ، لا يتم تنفيذ نص الحلقة ويذهب تدفق عنصر التحكم إلى التكرار التالي (تكرار عملية).
### تحديث
يستخدم بيان التحديث لتغيير متغير الحلقة باستخدام عمليات بسيطة مثل الجمع أو الطرح أو الضرب أو القسمة. ينفّذ بيان التحديث بعد تنفيذ نص الحلقة.
## التنفيذ:
`#include<iostream>
using namespace std; // Here we use the scope resolution operator to define the scope of the standar functions as std::
int main () {
// for loop execution
for( int a = 10; a <20;a =a+1){//Theloopwillruntillthevalueofaislessthan20
cout << "value of a: " <<a<<endl;
}
return 0;
}```
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
##Single lined loop
The body of the for loop need not be enclosed in braces if the loop iterates over only one satatement.
##Example
`
ج ++ #تتضمن استخدام اسم للمحطة؛
انت مين () { // خط واحد للحلقة لـ (int a = 10؛ a <20؛a =a+1)cout<<"قيمةa:"<<a<<endl؛
العودة 0 } \`\` \`
هذا من شأنه أن يولد نفس الناتج مثل البرنامج السابق. أي انتاج: قيمة أ: 10 قيمة أ: 11 قيمة أ: 12 قيمة أ: 13 قيمة a: 14 قيمة أ: 15 قيمة أ: 16 قيمة أ: 17 قيمة أ: 18 قيمة من: 19
`## Explanation
Here's the initialization condition is first set to a=10. The loop first checks for this condition. It then checks for the condition expression ie a<20whichholdstrueas10<20(forthefirstcase).Nowthebodyoftheloopisexecutedandwegettheoutput"Valueofa:10".Thentheupdateexpressionisexecutedwhichaddsthenumber1to'a'andthevalueof'a'getsupdatedto11andthesamestepsarefollowed(asabove)untilthevalueofvreacheslessthan20ie19.
# Range-based for-loop
C++ also has what we call range-based for loops which iterates through all the elements of a container(eg array).
## Syntax
`
لـ (العنصر: الحاوية) صياغات)؛ }
int \[5\] array = {1، 2، 3، 4، 5} لـ (int i: array) cout <<i<<endl؛}