logo

For vs. while حلقة في C

فهم الفرق بين حلقة for وحلقة while

تسمح عبارات التكرار في لغة C++، مثل حلقة for، و while، و do-while، بتنفيذ مجموعة من التعليمات بشكل متكرر حتى يصبح الشرط صحيحًا، ثم تنتهي عندما يكون الشرط خاطئًا. يمكن أن تحتوي عبارات التكرار على شروط محددة مسبقًا، كما هو الحال في حلقة for، أو شروط مفتوحة، كما هو الحال في حلقة while.

في لغة C++، يتم تضمين مجموعة متنوعة من أشكال حلقة 'for' لزيادة قابلية تطبيق اللغة وقوتها ومرونتها. على سبيل المثال، تسمح لنا حلقة for بالتحكم في الحلقة باستخدام متغيرات متعددة بداخلها، بالإضافة إلى استخدام دالة التقارب مع حلقة 'for'. في المقابل، لا يمكننا استخدام العديد من الاختلافات مع حلقة while؛ يجب استخدامه مع بناء الجملة القياسي.

هناك بعض الاختلافات الهامة بين حلقات for و while، والتي يتم شرحها بشكل أكبر باستخدام مخطط المقارنة.

For vs. while حلقة في C

يتم تعريف الحلقة على أنها

هناك نوعان من الحلقات في Java. الأول هو النموذج 'التقليدي'، والثاني هو النموذج 'لكل'.

الشكل الأكثر عمومية لبيان الحلقة.

 for (initialization; condition; iteration) { //body of for loop } 
    التهيئة:تتم تهيئة متغير التحكم في حلقة for مرة واحدة فقط، أثناء التكرار الأول للحلقة. تتم تهيئة متغير التحكم في الحلقة هنا؛ إذا لم يتم استخدام متغير الحلقة مرة أخرى في البرنامج وتم استخدامه فقط كمتغير متحكم في الحلقة، فسيتم الإعلان عنه وتهيئته في الحلقة 'for'.حالة:يتم تنفيذ شرط الحلقة 'for' في كل مرة يتم فيها تكرار الحلقة.
  • بيان التكرار هو تعبير يزيد أو ينقص متغير التحكم في الحلقة.

عند تنفيذ الحلقة، يتم تنفيذ شرط التهيئة أولاً، يليه التحقق من الحالة. إذا تم استيفاء الشرط، فسيتم تنفيذ نص الحلقة متبوعًا ببيان التكرار. يتم بعد ذلك التحقق من الحالة مرة أخرى لتحديد ما إذا كانت الحلقة ستتكرر أكثر أو ستنتهي.

جافا مقارنة السلاسل

في Java، يمكن أن تحتوي عبارات التهيئة والتكرار على عبارات متعددة. فاصلة تفصل بين كل عبارة. في Java، الفاصلة هي فاصل؛ في لغة C++، الفاصلة هي عامل تشغيل يمكن استخدامه في أي تعبير صالح.

بناء جملة حلقة for-each

يعد نموذج 'لكل' نسخة أكثر تقدمًا من حلقة for. تأخذ حلقة for-each الشكل العام التالي.

 for(type iter_variable: collection) statement-block 

تحدد المعلمة 'type' نوع متغير التكرار، والذي يتبعه متغير التكرار. سيتم تمرير العنصر من متغير المجموعة إلى متغير التكرار. يجب أن يتطابق النوع مع نوع العناصر الموجودة في متغير المجموعة. يقوم نموذج for-each من حلقة for بأتمتة تكرار الحلقة من البداية إلى النهاية، والوصول إلى القيم بترتيب تسلسلي.

مثال

هناك أنواع مختلفة من المجموعات التي يمكن استخدامها مع حلقة for. دعونا نتحدث عن ذلك باستخدام مجموعة كمجموعة.

 public class Main { public static void main(String[] args) { int array[]={10, 20, 30, 40, 50, 60}; int add=0; for( int c: array) { System.out.println( 'value in c ' + c); add = add+c; } System.out.println('additon of array elements is ' +add); } } 

انتاج:

 value in c 10 value in c 20 value in c 30 value in c 40 value in c 50 value in c 60 additon of array elements is 210 

'c' هو متغير التكرار في هذه الحالة؛ يتلقى القيم من المصفوفة[]، واحدة تلو الأخرى، من الأدنى إلى الأعلى في المصفوفة. تتكرر الحلقة حتى يتم فحص كافة عناصر المصفوفة. يمكن كسر الحلقة من المنتصف باستخدام كلمة 'break'. من ناحية أخرى، فإن التغيير في متغير التكرار ليس له أي تأثير على المصفوفة لأنه متغير للقراءة فقط.

بينما يتم تعريف الحلقة على أنها

حلقة while هي الحلقة الأساسية في كل من C++ وJava. عملية حلقة while مشابهة في C++ وJava.

بناء الجملة

ما يلي هو إعلان حلقة while:

 while ( condition) { statements; //body of loop } 

تتحقق حلقة while أولاً من الشرط ثم تنفذ العبارات حتى يصبح الشرط الموجود في حلقة while صحيحًا. في حلقة while، يمكن أن يكون الشرط أي تعبير منطقي. عندما يُرجع تعبير قيمة غير الصفر، يكون الشرط صحيحًا؛ وعندما تُرجع قيمة صفر، يكون الشرط خاطئًا.

إذا كان الشرط صحيحا، فإن الحلقة تكرر نفسها؛ إذا كان الشرط خاطئًا، فسيتم تمرير التحكم إلى سطر التعليمات البرمجية مباشرة بعد الحلقة. يمكن أن تكون حلقة النص أو العبارات عبارة فارغة، أو عبارة واحدة، أو مجموعة من العبارات.

مثال

دعونا نلقي نظرة على كيفية عمل حلقة while. سيتم طباعة الكود الموجود في المثال أدناه من 1 إلى 10.

 public class Main { public static void main (String args[]) { int n=0; while(n<10) { n++; system.out.println('n=" +n); } } } &lt;/pre&gt; &lt;p&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;/p&gt; &lt;pre&gt; n=1 n=2 n=3 n=4 n=5 n=6 n=7 n=8 n=9 n=10 &lt;/pre&gt; &lt;p&gt;The initial value of " n' in this case is 0, which makes the condition while loop true. control then enters loop's body, where value of 'n' incremented accordance with first statement.< p> <p>The value of &apos;n&apos; is printed, then control returns to the condition in a while loop, where the value of &apos;n&apos; is now 1, satisfying the condition once more, and the body of the loop is executed once more. This continues until the condition becomes false, at which point the loop is terminated.</p> <p>The &apos;while&apos; loop, like the &apos;for&apos; loop, can initialise the control variable at the beginning of the loop, i.e. during condition checking.</p> <pre> //for example while((ch = getchar( ) ) != &apos;A&apos;) { System.out.println(&apos; The input alphabet &apos; +ch); } </pre> <p>At the top of the loop, the control variable &apos;ch&apos; is initialised, and the loop&apos;s condition is verified.</p> <h4>Note: If there is only one statement in the body of the loop, whether it is a for loop or a while loop, the curly braces are not required.</h4> <h3>In C, what is the difference between a for loop and a while?</h3> <table class="table"> <tr> <th>Parameters</th> <th>For Loop</th> <th>While Loop</th> </tr> <tr> <td> <strong>Declaration</strong> </td> <td>for(initialization ; condition ; iteration ) { <br> //body of &apos;for&apos; loop <br> }</td> <td>initialization <br>while ( condition ) { <br>statements; <br>//body of loop <br>}</td> </tr> <tr> <td> <strong>Format.</strong> </td> <td>At the top of the loop, initialization, condition checking, and iteration statements are written.</td> <td>At the top of the loop, only initialization and condition checking are performed.</td> </tr> <tr> <td> <strong>Use.</strong> </td> <td>The &apos;for&apos; loop was only used when the number of iterations was already known.</td> <td>When the number of iterations is unknown, the &apos;while&apos; loop is used.</td> </tr> <tr> <td> <strong>Condition.</strong> </td> <td>If the condition is not included in the &apos;for&apos; loop, the loop iterates indefinitely.</td> <td>If the condition is not included in the &apos;while&apos; loop, a compilation error occurs.</td> </tr> <tr> <td> <strong>Initialization</strong> </td> <td>The initialization is never repeated in a &apos;for&apos; loop.</td> <td>If initialization is performed during condition checking in a while loop, initialization is performed each time the loop iterates.</td> </tr> <tr> <td> <strong>Iteration assertion</strong> </td> <td>Because the iteration statement in the &apos;for&apos; loop is written at the top, it executes only after all statements in the loop have been executed.</td> <td>The iteration statement in a &apos;while&apos; loop can be written anywhere in the loop.</td> </tr> </table> <h2>The Key Differences Between for and while loop</h2> <ul> <li>Initialization, condition checking, and increment or decrement of iteration variables are all done explicitly in the loop syntax only. In contrast, in the while loop, we can only initialise and check the condition in the loop syntax.</li> <li>When we know the number of iterations that must occur in a loop execution, we use the for loop. On the other hand, if we do not know how many iterations must occur in a loop, we use a while loop.</li> <li>If you do not include a condition statement in the for loop, the loop will loop indefinitely. In contrast, failing to include a condition statement in the while loop will result in a compilation error.</li> <li>The initialization statement in the for loop syntax is only executed once at the beginning of the loop. If the while loop&apos;s syntax includes an initialization statement, the initialization statement will be executed each time the loop iterates.</li> <li>The iteration statement in the for loop will run after the body of the for loop. On the contrary, because the iteration statement can be written anywhere in the body of the while loop, there may be some statements that execute after the iteration statement in the body of the while loop is executed.</li> </ul> <h2>Conclusion</h2> <p>Loops are thus a collection of commands that must be used in a specific order. If the loop structure is incorrect, the programming will display the syntax error. Loops run to obtain a result or to satisfy a condition or set of conditions. It is the foundation of all programming languages.</p> <p>During execution, the loop structure asks a question and executes until the answer is satisfactory. The same question is asked again and again until the new statement is applied. The looping process continues indefinitely until the programme reaches a breakpoint. In the event that the breaking point is not reached, the programme will crash.</p> <p>The for and while loops are both conditional statements. A for loop is a single-line command that will be executed repeatedly. While loops can be single-lined or contain multiple commands for a single condition.</p> <p>Both the for loop and the while loop are important in computer languages for obtaining results. The condition is met if the command syntax is correct.</p> <p>Both the for loop and the while loop are iteration statements, but they have distinct characteristics. The for loop declares everything (initialization, condition, iteration) at the top of the loop body. In contrast, only initialization and condition are at the top of the body of the loop in a while loop, and iteration can be written anywhere in the body of the loop.</p> <hr></10)>

في الجزء العلوي من الحلقة، تتم تهيئة متغير التحكم 'ch'، ويتم التحقق من حالة الحلقة.

ملاحظة: إذا كان هناك عبارة واحدة فقط في نص الحلقة، سواء كانت حلقة for أو while، فلن تكون هناك حاجة للأقواس المتعرجة.

في لغة C، ما الفرق بين حلقة for و while؟

حدود لحلقة حائط اللوب
تصريح ل(التهيئة ؛ الحالة ؛ التكرار) {
// نص الحلقة 'for'.
}
التهيئة
بينما (الشرط) {
صياغات؛
// جسم الحلقة
}
شكل. في الجزء العلوي من الحلقة، تتم كتابة بيانات التهيئة والتحقق من الحالة والتكرار. في الجزء العلوي من الحلقة، يتم تنفيذ التهيئة والتحقق من الحالة فقط.
يستخدم. تم استخدام حلقة 'for' فقط عندما يكون عدد التكرارات معروفًا بالفعل. عندما يكون عدد التكرارات غير معروف، يتم استخدام حلقة 'while'.
حالة. إذا لم يتم تضمين الشرط في حلقة 'for'، فستتكرر الحلقة إلى أجل غير مسمى. إذا لم يتم تضمين الشرط في حلقة 'أثناء'، يحدث خطأ في الترجمة.
التهيئة لا يتم تكرار التهيئة مطلقًا في حلقة 'for'. إذا تم تنفيذ التهيئة أثناء التحقق من الحالة في حلقة while، فسيتم إجراء التهيئة في كل مرة تتكرر فيها الحلقة.
تأكيد التكرار نظرًا لأن عبارة التكرار في الحلقة 'for' مكتوبة في الأعلى، فإنها يتم تنفيذها فقط بعد تنفيذ جميع العبارات الموجودة في الحلقة. يمكن كتابة عبارة التكرار في حلقة 'while' في أي مكان في الحلقة.

الاختلافات الرئيسية بين حلقة for و while

  • يتم إجراء التهيئة والتحقق من الحالة وزيادة أو تقليل متغيرات التكرار بشكل صريح في بناء جملة الحلقة فقط. في المقابل، في الحلقة while، يمكننا فقط تهيئة الحالة والتحقق منها في بناء جملة الحلقة.
  • عندما نعرف عدد التكرارات التي يجب أن تحدث في تنفيذ الحلقة، نستخدم حلقة for. من ناحية أخرى، إذا كنا لا نعرف عدد التكرارات التي يجب أن تحدث في الحلقة، فإننا نستخدم حلقة while.
  • إذا لم تقم بتضمين بيان شرط في حلقة for، فسوف تتكرر الحلقة إلى أجل غير مسمى. في المقابل، الفشل في تضمين عبارة شرطية في حلقة while سيؤدي إلى خطأ في الترجمة.
  • يتم تنفيذ عبارة التهيئة في بناء جملة الحلقة مرة واحدة فقط في بداية الحلقة. إذا كان بناء جملة الحلقة يتضمن بيان تهيئة، فسيتم تنفيذ بيان التهيئة في كل مرة تتكرر فيها الحلقة.
  • سيتم تشغيل عبارة التكرار في حلقة for بعد نص الحلقة. على العكس من ذلك، نظرًا لأنه يمكن كتابة عبارة التكرار في أي مكان في نص الحلقة، فقد تكون هناك بعض العبارات التي يتم تنفيذها بعد تنفيذ عبارة التكرار في نص الحلقة.

خاتمة

وبالتالي فإن الحلقات هي مجموعة من الأوامر التي يجب استخدامها بترتيب معين. إذا كانت بنية الحلقة غير صحيحة، فستعرض البرمجة خطأ في بناء الجملة. يتم تشغيل الحلقات للحصول على نتيجة أو لتلبية شرط أو مجموعة من الشروط. وهي أساس جميع لغات البرمجة.

أثناء التنفيذ، يطرح هيكل الحلقة سؤالاً ويتم تنفيذه حتى تصبح الإجابة مرضية. يتم طرح نفس السؤال مرارا وتكرارا حتى يتم تطبيق البيان الجديد. تستمر عملية التكرار إلى أجل غير مسمى حتى يصل البرنامج إلى نقطة التوقف. وفي حالة عدم الوصول إلى نقطة الانهيار، سيتعطل البرنامج.

معدد

تعتبر حلقات for و while عبارة عن عبارات شرطية. حلقة for هي أمر من سطر واحد سيتم تنفيذه بشكل متكرر. يمكن أن تكون حلقات while سطرًا واحدًا أو تحتوي على أوامر متعددة لشرط واحد.

تعتبر كل من حلقة for وحلقات while مهمة في لغات الكمبيوتر للحصول على النتائج. يتم استيفاء الشرط إذا كان بناء جملة الأمر صحيحًا.

كل من حلقة for وحلقة while عبارة عن عبارات تكرارية، لكن لهما خصائص مميزة. تعلن حلقة for عن كل شيء (التهيئة، الحالة، التكرار) في الجزء العلوي من نص الحلقة. في المقابل، توجد التهيئة والحالة فقط في الجزء العلوي من جسم الحلقة في حلقة while، ويمكن كتابة التكرار في أي مكان في جسم الحلقة.