حلقات for
مُقدَّم
7:49
الاختيار والتكرار
إليك ثلاث حلقات تكرار. الأولى اثنان differs by one character — a less-than instead of a less-than-or-equal. The first runs ten times. The second runs eleven.…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| for loop/fɔː luːp/ | حلقة for |
A loop with a counter built in
- A for loop 计数循环 packs three parts into one line:
for (int i = 0; i < 5; i++) { ... }- Initialization (
int i = 0), condition (i < 5), update (i++). - Use
forwhen you know how many times to repeat.
The three parts, in order
- Initialization runs once, before the loop starts.
- The condition is checked before each pass (like
while). - The update runs after each pass, then the condition is re-checked.
- So the flow is: init → (test → body → update) → (test → body → update) → …

The three parts of a for loop and the order they run
for and while are interchangeable
- Any
forloop can be rewritten as awhileloop, and vice versa. - If
Bhas nocontinuetargeting this loop,for (int i=0; i<5; i++) {B}can be rewritten as{ int i=0; while (i<5) {B; i++;} }. foris tidier when the counter setup, test, and change belong together.- Choose whichever makes the loop's intent clearest.
Counting a known number of times
for (int i = 0; i < n; i++)runs the body exactlyntimes (i=0ton-1).- Change the bounds to count differently:
i = 1; i <= nrunsntimes too. - Watch the boundary:
<stops one before,<=includes the end. - This is the standard pattern for "do something
ntimes."
Mind the loop bounds — an off-by-one changes the count. for (int i = 0; i < n; i++) runs n times (i from 0 to n-1); switching to i <= n runs n+1 times and often overruns an array. Trace the first and last value of i to confirm the count before trusting a for loop.
Printing 0,1,2,3,4:
for (int i = 0; i < 5; i++) { System.out.println(i); }istarts0, runs whilei < 5,i++each pass.- Prints
0 1 2 3 4(five values); ati = 5the condition fails → stop.
A for loop combines initialization (once), a condition (checked before each pass), and an update (after each pass): for (int i = 0; i < n; i++) runs the body n times. It's interchangeable with a while loop. Mind the bounds — < vs <= changes the count by one.
حلقة for تحسب من 0 إلى 4
i تعمل من 0..4 (خمس مرات); عند i=5 يفشل الشرط.
الأجزاء الثلاثة لرأس حلقة for هي، بالترتيب...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
for (init; condition; update).
كم مرة سيعمل جسم حلقة for (int i = 0; i < 5; i++)؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
i تأخذ القيم 0,1,2,3,4 → 5 مرات.
جزء التهيئة في حلقة for يعمل...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Init تعمل مرة واحدة؛ يتم التحقق من الشرط في كل مرة؛ update تعمل بعد كل مرة.
يمكن إعادة كتابة أي حلقة for كحلقة while مكافئة.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
for و while قابلتان للتبديل.
كم مرة ستعمل for (int i = 1; i <= 5; i++)؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
i تأخذ القيم 1..5 (شاملة) → 5 مرات.
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.