حلقة for
حلقة for
لا تتضمن Go سوى بنية واحدة للحلقات، وهي حلقة for.
تتكون حلقة for الأساسية من ثلاثة مكونات تفصل بينها فواصل منقوطة:
- تعليمة التهيئة: تُنفَّذ قبل التكرار الأول
- تعبير الشرط: يُقيَّم قبل كل تكرار
- التعليمة اللاحقة: تُنفَّذ في نهاية كل تكرار
غالبًا ما تكون تعليمة التهيئة تصريحًا عن متغير، ولا تكون
المتغيرات المُصرَّح عنها هناك مرئية إلا ضمن نطاق تعليمة for
وحدها.
تتوقف الحلقة عن التكرار بمجرد أن تصبح قيمة الشرط المنطقي false.
ملاحظة: على خلاف لغات أخرى مثل C أو Java أو JavaScript، لا توجد أقواس مستديرة
تحيط بالمكونات الثلاثة لتعليمة for، أما الأقواس المعقوفة { } فهي
مطلوبة دائمًا.
النص الأصلي بالإنجليزية
For
Go has only one looping construct, the for loop.
The basic for loop has three components separated by semicolons:
- the init statement: executed before the first iteration
- the condition expression: evaluated before every iteration
- the post statement: executed at the end of every iteration
The init statement will often be a variable declaration, and the
variables declared there are visible only in the scope of the for
statement.
The loop will stop iterating once the boolean condition evaluates to false.
Note: Unlike other languages like C, Java, or JavaScript there are no parentheses
surrounding the three components of the for statement and the braces { } are
always required.
هذا الدرس للقراءة فقط؛ جرّب المثال في الدرس التالي.