أ حلقة عبارة عن بنية تحكم في البرمجة تسمح لك بتنفيذ كتلة من التعليمات البرمجية إلى أجل غير مسمى إذا تم استيفاء شرط معين. تُستخدم الحلقات لتنفيذ الأنشطة المتكررة وتعزيز أداء البرمجة. هناك حلقات متعددة في لغة البرمجة C، واحدة منها هي حلقة 'افعل أثناء'. .
أ حلقة 'افعل أثناء'. هو شكل من أشكال أ حلقة في C الذي ينفذ كتلة التعليمات البرمجية أولا، تليها الشرط. إذا كان الشرط حقيقي ، ال حلقة يستمر في الجري؛ وإلا فإنه يتوقف. ومع ذلك، ما إذا كان الشرط هو في الأصل حقيقي فهو يضمن تنفيذ كتلة التعليمات البرمجية مرة واحدة على الأقل.
افعل أثناء بناء جملة الحلقة
بناء جملة حلقة do-while في لغة C موضح أدناه:
do{ //code to be executed }while(condition);
وتنقسم المكونات إلى ما يلي:
سلسلة في شار جافا
- ال افعل الكلمة الرئيسية يمثل بداية الحلقة.
- ال كتلة التعليمات البرمجية داخل الأقواس المعقوفة {} هو نص الحلقة، والذي يحتوي على الكود الذي تريد تكراره.
- ال بينما الكلمة الرئيسية يتبعه شرط بين قوسين (). بعد تشغيل كتلة التعليمات البرمجية، يتم التحقق من هذا الشرط. إذا كان الشرط حقيقي ، تستمر الحلقة في مكان آخر، تنتهي الحلقة .
العمل أثناء تنفيذ Loop في C
دعونا نلقي نظرة على مثال لكيفية أ افعل بينما حلقة يعمل بلغة C. في هذا المثال، سنكتب برنامجًا بسيطًا يطرح على المستخدم أسئلة حول كيفية تنفيذ الأمر كلمة المرور ويستمر في السؤال حتى يتم إدخال كلمة المرور الصحيحة.
مثال:
#include #include int main() { char password[] = 'secret'; char input[20]; do { printf('Enter the password: '); scanf('%s', input); } while (strcmp(input, password) != 0); printf('Access granted! '); return 0; }
يعمل البرنامج على النحو التالي:
- يتم تضمين ملفات الرأس التالية: للمعيار مدخل و انتاج الروتين و للسلسلة وظائف التلاعب .
- يتم تعريف كلمة المرور الصحيحة على أنها أ مصفوفة الأحرف (كلمة مرور char[]) مع القيمة 'سر'
- بعد ذلك، نقوم بتحديد إدخال مصفوفة أحرف أخرى لتخزين مدخلات المستخدم.
- ال افعل الكلمة الرئيسية يشير إلى أن كتلة التعليمات البرمجية المضمنة داخل حلقة سيتم تنفيذها مرة واحدة على الأقل.
- باستخدام وظيفة برينتف (). ، نعرض مطالبة تطلب من المستخدم إدخال كلمة المرور الخاصة به داخل الحلقة.
- التالي نقرأ إدخال المستخدم باستخدام وظيفة سكانف (). وتخزينها في مصفوفة الإدخال .
- بعد قراءة مدخل ، نحن نستخدم ال الدالة strcmp() لمقارنة الإدخال بكلمة المرور الصحيحة. إذا كانت السلاسل متساوي، ال وظيفة ستركمب تُرجع 0. لذا، سنستمر في التكرار طالما أن الإدخال وكلمة المرور غير متساويين.
- مرة واحدة في كلمة المرور الصحيحة يتم إدخالها، وتنتهي الحلقة، ونقوم بالطباعة 'لقد تم منح الوصول!' باستخدام وظيفة برينتف (). .
- بعد ذلك، يقوم البرنامج بإرجاع 0 للإشارة إلى نجاح التنفيذ.
انتاج:
دعونا نسير عبر السيناريو المحتمل:
Enter the password: 123 Enter the password: abc Enter the password: secret Access Granted!
توضيح:
في هذا المثال، يقوم المستخدم في البداية بإدخال كلمات مرور خاطئة، '123' و 'اي بي سي' . تطالب الحلقة المستخدم بالحصول على كلمة المرور الصحيحة 'سر' تم إدخاله. بمجرد توفير كلمة المرور الصحيحة، تنتهي الحلقة، ويتم 'لقد تم منح الوصول!' يتم عرض الرسالة.
مثال على تنفيذ حلقة في C:
مثال 1:
هنا مثال بسيط على أ حلقة 'افعل أثناء'. في C الذي يطبع الأرقام من 1 إلى 5:
#include int main() { inti = 1; do { printf('%d ', i); i++; } while (i<= 5); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, the <strong> <em>code block</em> </strong> within the do loop will be executed at least once, printing numbers from <strong> <em>1 to 5</em> </strong> . After each iteration, the <strong> <em>i value</em> </strong> is incremented, and the condition <strong> <em>i<= 5< em> </=></em></strong> is checked. If the condition is still true, the loop continues; otherwise, it terminates.</p> <p> <strong>Example 2:</strong> </p> <p>Program to print table for the given number using do while Loop</p> <pre> #include intmain(){ inti=1,number=0; printf('Enter a number: '); scanf('%d',&number); do{ printf('%d ',(number*i)); i++; }while(i<=10); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number: 5 5 10 15 20 25 30 35 40 45 50 Enter a number: 10 10 20 30 40 50 60 70 80 90 100 </pre> <p> <strong>Example 3:</strong> </p> <p>Let's take a program that prints the multiplication table of a given number N using a <strong> <em>do...while Loop</em> :</strong> </p> <pre> #include int main() { int N; printf('Enter a number to generate its multiplication table: '); scanf('%d', &N); inti = 1; do { printf('%d x %d = %d ', N, i, N * i); i++; } while (i<= 10); return 0; } < pre> <p> <strong>Output:</strong> </p> <p>Let us say you enter the number 7 as input:</p> <pre> Please enter a number to generate its multiplication table: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 </pre> <p>The program calculates and prints the multiplication table for <strong> <em>7</em> </strong> from 1 to 10.</p> <h3>Infinite do while loop</h3> <p>An <strong> <em>infinite loop</em> </strong> is a loop that runs indefinitely as its condition is always <strong> <em>true</em> </strong> or it lacks a terminating condition. Here is an example of an <strong> <em>infinite do...while loop</em> </strong> in C:</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { inti = 1; do { printf('Iteration %d ', i); i++; } while (1); // Condition is always true return 0; } </pre> <p>In this <strong> <em>example</em> </strong> , the <strong> <em>loop</em> </strong> will keep running <strong> <em>indefinitely</em> </strong> because <strong> <em>condition 1</em> </strong> is always <strong> <em>true</em> </strong> .</p> <p> <strong>Output:</strong> </p> <p>When you run the program, you will see that it continues printing <strong> <em>'Iteration x',</em> </strong> where x is the <strong> <em>iteration number</em> </strong> without stopping:</p> <pre> Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on) </pre> <p>To interrupt an infinite loop like this, you generally use a <strong> <em>break statement</em> </strong> within the <strong> <em>loop</em> </strong> or some external condition you can control, such as <strong> <em>hitting</em> </strong> a specific key combination. In most desktop settings, the keyboard shortcut <strong> <em>Ctrl+C</em> </strong> can escape the Loop.</p> <h3>Nested do while loop in C</h3> <p>In C, we take an example of a <strong> <em>nested do...while loop</em> </strong> . In this example, we will write a program that uses <strong> <em>nested do...while loops</em> </strong> to create a numerical pattern.</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { int rows, i = 1; printf('Enter the number of rows: '); scanf('%d', &rows); do { int j = 1; do { printf('%d ', j); j++; } while (j <= i); printf(' '); i++; } while (i<="rows);" return 0; < pre> <p>In this program, we use <strong> <em>nested do...while loops</em> </strong> to generate a pattern of numbers. The <strong> <em>outer loop</em> </strong> controls the number of rows, and the <strong> <em>inner loop</em> </strong> generates the numbers for each row.</p> <p> <strong>Output:</strong> </p> <p>Let us say you input five as the number of rows:</p> <pre> Enter the number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, the program generates a pattern of numbers in a <strong> <em>triangular shape</em> </strong> . The <strong> <em>outer loop</em> </strong> iterates over the rows, and the <strong> <em>inner loop</em> </strong> iterates within each row, printing the numbers from 1 up to the current row number.</p> <h2>Difference between while and do while Loop</h2> <p>Here is a tabular comparison between the while loop and the do-while Loop in C:</p> <table class="table"> <tr> <th>Aspect</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td> <strong>Syntax</strong> </td> <td>while (condition) { ... }</td> <td>do { ... } while (condition);</td> </tr> <tr> <td> <strong>Loop Body Execution</strong> </td> <td>Condition is checked before execution.</td> <td>The body is executed before the condition.</td> </tr> <tr> <td> <strong>First Execution</strong> </td> <td>The condition must be true initially.</td> <td>The body is executed at least once.</td> </tr> <tr> <td> <strong>Loop Execution</strong> </td> <td>May execute zero or more times.</td> <td>Will execute at least once.</td> </tr> <tr> <td> <strong>Example</strong> </td> <td>while (i<5) { printf('%d ', i); i++; }< td> <td>do { printf('%d ', i); i++; } while (i<5);< td> </5);<></td></5)></td></tr> <tr> <td> <strong>Common Use Cases</strong> </td> <td>When the loop may not run at all.</td> <td>When you want the loop to run at least once.</td> </tr> </table> <p> <strong>While Loop:</strong> The loop body is executed before the condition is checked. If the condition is initially <strong> <em>false</em> </strong> , the loop may not execute.</p> <p> <strong>Do-while Loop:</strong> The <strong> <em>loop body</em> </strong> is executed at least once before the condition is <strong> <em>checked</em> </strong> . This guarantees that the loop completes at least one iteration.</p> <p>When you want the <strong> <em>loop</em> </strong> to run based on a condition that may be <strong> <em>false</em> </strong> at first, use the <strong> <em>while loop</em> </strong> , and when you want the loop to run at least once regardless of the starting state, use the <strong> <em>do-while loop.</em> </strong> </p> <h2>Features of do while loop</h2> <p>The do-while loop in C has several fundamental characteristics that make it an effective programming technique in certain situations. The following are the significant characteristics of the do-while loop:</p> <ul> <tr><td>Guaranteed Execution:</td> Unlike other <strong> <em>loop structures</em> </strong> , the <strong> <em>do-while oop</em> </strong> ensures that the loop body is executed at least once. Because the condition is assessed after the loop body, the code within the loop is performed before the condition is verified. </tr><tr><td>Loop after testing:</td> The <strong> <em>do-while loop</em> </strong> is a post-tested loop which implies that the loop condition is assessed after the loop body has been executed. If the condition is true, the loop body is run once again. This behavior allows you to verify the condition for repetition before ensuring that a given activity is completed. </tr><tr><td>Conditionally Controlled:</td> The loop continues to execute as long as the condition specified after the while keyword remains <strong> <em>true</em> </strong> . When the condition evaluates to <strong> <em>false</em> </strong> , the loop is terminated, and control shifts to the sentence after the loop. </tr><tr><td>Flexibility:</td> The <strong> <em>do-while loop</em> </strong> may be utilized in several contexts. It is typically used in cases where a piece of code must be executed at least once, such as <strong> <em>menu-driven programs, input validation,</em> </strong> or <strong> <em>repetitive computations</em> </strong> . </tr><tr><td>Nesting Capability:</td> Similar to other <strong> <em>loop constructs</em> </strong> , the <strong> <em>do-while loop</em> </strong> can be <strong> <em>nested</em> </strong> inside other <strong> <em>loops</em> </strong> or <strong> <em>control structures</em> </strong> to create more complex control flow patterns. It allows for the creation of <strong> <em>nested loops</em> </strong> and the implementation of intricate repetitive tasks. </tr><tr><td>Break and Continue:</td> The break statement can be used within a <strong> <em>do-while loop</em> </strong> to terminate the loop execution and exit the loop prematurely. The <strong> <em>continue statement</em> </strong> can skip the remaining code in the current iteration and jump to the next iteration of the loop. </tr><tr><td>Local Scope:</td> Variables declared inside the <strong> <em>do-while loop</em> </strong> body have local scope and are accessible only within the <strong> <em>loop block.</em> </strong> They cannot be accessed outside the loop or by other loops or control structures. </tr><tr><td>Infinite Loop Control:</td> It is crucial to ensure that the loop's condition is eventually modified within the <strong> <em>loop body</em> </strong> . This modification is necessary to prevent infinite loops where the condition continually evaluates to true. Modifying the condition ensures that the loop terminates at some point. </tr></ul> <hr></=></pre></=></pre></=10);></pre></=>
توضيح:
في هذا المثال، كتلة التعليمات البرمجية سيتم تنفيذه داخل حلقة do مرة واحدة على الأقل، وطباعة الأرقام من 1 إلى 5 . بعد كل تكرار، أقيم يتم الزيادة، والشرط أنا<= 5< em> => مفحوص. إذا كان الشرط لا يزال صحيحا، تستمر الحلقة؛ وإلا فإنه ينتهي.
مثال 2:
برنامج لطباعة جدول للرقم المحدد باستخدام do while Loop
#include intmain(){ inti=1,number=0; printf('Enter a number: '); scanf('%d',&number); do{ printf('%d ',(number*i)); i++; }while(i<=10); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> Enter a number: 5 5 10 15 20 25 30 35 40 45 50 Enter a number: 10 10 20 30 40 50 60 70 80 90 100 </pre> <p> <strong>Example 3:</strong> </p> <p>Let's take a program that prints the multiplication table of a given number N using a <strong> <em>do...while Loop</em> :</strong> </p> <pre> #include int main() { int N; printf('Enter a number to generate its multiplication table: '); scanf('%d', &N); inti = 1; do { printf('%d x %d = %d ', N, i, N * i); i++; } while (i<= 10); return 0; } < pre> <p> <strong>Output:</strong> </p> <p>Let us say you enter the number 7 as input:</p> <pre> Please enter a number to generate its multiplication table: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 </pre> <p>The program calculates and prints the multiplication table for <strong> <em>7</em> </strong> from 1 to 10.</p> <h3>Infinite do while loop</h3> <p>An <strong> <em>infinite loop</em> </strong> is a loop that runs indefinitely as its condition is always <strong> <em>true</em> </strong> or it lacks a terminating condition. Here is an example of an <strong> <em>infinite do...while loop</em> </strong> in C:</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { inti = 1; do { printf('Iteration %d ', i); i++; } while (1); // Condition is always true return 0; } </pre> <p>In this <strong> <em>example</em> </strong> , the <strong> <em>loop</em> </strong> will keep running <strong> <em>indefinitely</em> </strong> because <strong> <em>condition 1</em> </strong> is always <strong> <em>true</em> </strong> .</p> <p> <strong>Output:</strong> </p> <p>When you run the program, you will see that it continues printing <strong> <em>'Iteration x',</em> </strong> where x is the <strong> <em>iteration number</em> </strong> without stopping:</p> <pre> Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on) </pre> <p>To interrupt an infinite loop like this, you generally use a <strong> <em>break statement</em> </strong> within the <strong> <em>loop</em> </strong> or some external condition you can control, such as <strong> <em>hitting</em> </strong> a specific key combination. In most desktop settings, the keyboard shortcut <strong> <em>Ctrl+C</em> </strong> can escape the Loop.</p> <h3>Nested do while loop in C</h3> <p>In C, we take an example of a <strong> <em>nested do...while loop</em> </strong> . In this example, we will write a program that uses <strong> <em>nested do...while loops</em> </strong> to create a numerical pattern.</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { int rows, i = 1; printf('Enter the number of rows: '); scanf('%d', &rows); do { int j = 1; do { printf('%d ', j); j++; } while (j <= i); printf(\' \'); i++; } while (i<="rows);" return 0; < pre> <p>In this program, we use <strong> <em>nested do...while loops</em> </strong> to generate a pattern of numbers. The <strong> <em>outer loop</em> </strong> controls the number of rows, and the <strong> <em>inner loop</em> </strong> generates the numbers for each row.</p> <p> <strong>Output:</strong> </p> <p>Let us say you input five as the number of rows:</p> <pre> Enter the number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, the program generates a pattern of numbers in a <strong> <em>triangular shape</em> </strong> . The <strong> <em>outer loop</em> </strong> iterates over the rows, and the <strong> <em>inner loop</em> </strong> iterates within each row, printing the numbers from 1 up to the current row number.</p> <h2>Difference between while and do while Loop</h2> <p>Here is a tabular comparison between the while loop and the do-while Loop in C:</p> <table class="table"> <tr> <th>Aspect</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td> <strong>Syntax</strong> </td> <td>while (condition) { ... }</td> <td>do { ... } while (condition);</td> </tr> <tr> <td> <strong>Loop Body Execution</strong> </td> <td>Condition is checked before execution.</td> <td>The body is executed before the condition.</td> </tr> <tr> <td> <strong>First Execution</strong> </td> <td>The condition must be true initially.</td> <td>The body is executed at least once.</td> </tr> <tr> <td> <strong>Loop Execution</strong> </td> <td>May execute zero or more times.</td> <td>Will execute at least once.</td> </tr> <tr> <td> <strong>Example</strong> </td> <td>while (i<5) { printf(\'%d \', i); i++; }< td> <td>do { printf('%d ', i); i++; } while (i<5);< td> </5);<></td></5)></td></tr> <tr> <td> <strong>Common Use Cases</strong> </td> <td>When the loop may not run at all.</td> <td>When you want the loop to run at least once.</td> </tr> </table> <p> <strong>While Loop:</strong> The loop body is executed before the condition is checked. If the condition is initially <strong> <em>false</em> </strong> , the loop may not execute.</p> <p> <strong>Do-while Loop:</strong> The <strong> <em>loop body</em> </strong> is executed at least once before the condition is <strong> <em>checked</em> </strong> . This guarantees that the loop completes at least one iteration.</p> <p>When you want the <strong> <em>loop</em> </strong> to run based on a condition that may be <strong> <em>false</em> </strong> at first, use the <strong> <em>while loop</em> </strong> , and when you want the loop to run at least once regardless of the starting state, use the <strong> <em>do-while loop.</em> </strong> </p> <h2>Features of do while loop</h2> <p>The do-while loop in C has several fundamental characteristics that make it an effective programming technique in certain situations. The following are the significant characteristics of the do-while loop:</p> <ul> <tr><td>Guaranteed Execution:</td> Unlike other <strong> <em>loop structures</em> </strong> , the <strong> <em>do-while oop</em> </strong> ensures that the loop body is executed at least once. Because the condition is assessed after the loop body, the code within the loop is performed before the condition is verified. </tr><tr><td>Loop after testing:</td> The <strong> <em>do-while loop</em> </strong> is a post-tested loop which implies that the loop condition is assessed after the loop body has been executed. If the condition is true, the loop body is run once again. This behavior allows you to verify the condition for repetition before ensuring that a given activity is completed. </tr><tr><td>Conditionally Controlled:</td> The loop continues to execute as long as the condition specified after the while keyword remains <strong> <em>true</em> </strong> . When the condition evaluates to <strong> <em>false</em> </strong> , the loop is terminated, and control shifts to the sentence after the loop. </tr><tr><td>Flexibility:</td> The <strong> <em>do-while loop</em> </strong> may be utilized in several contexts. It is typically used in cases where a piece of code must be executed at least once, such as <strong> <em>menu-driven programs, input validation,</em> </strong> or <strong> <em>repetitive computations</em> </strong> . </tr><tr><td>Nesting Capability:</td> Similar to other <strong> <em>loop constructs</em> </strong> , the <strong> <em>do-while loop</em> </strong> can be <strong> <em>nested</em> </strong> inside other <strong> <em>loops</em> </strong> or <strong> <em>control structures</em> </strong> to create more complex control flow patterns. It allows for the creation of <strong> <em>nested loops</em> </strong> and the implementation of intricate repetitive tasks. </tr><tr><td>Break and Continue:</td> The break statement can be used within a <strong> <em>do-while loop</em> </strong> to terminate the loop execution and exit the loop prematurely. The <strong> <em>continue statement</em> </strong> can skip the remaining code in the current iteration and jump to the next iteration of the loop. </tr><tr><td>Local Scope:</td> Variables declared inside the <strong> <em>do-while loop</em> </strong> body have local scope and are accessible only within the <strong> <em>loop block.</em> </strong> They cannot be accessed outside the loop or by other loops or control structures. </tr><tr><td>Infinite Loop Control:</td> It is crucial to ensure that the loop's condition is eventually modified within the <strong> <em>loop body</em> </strong> . This modification is necessary to prevent infinite loops where the condition continually evaluates to true. Modifying the condition ensures that the loop terminates at some point. </tr></ul> <hr></=></pre></=></pre></=10);>
مثال 3:
لنأخذ برنامجًا يطبع جدول الضرب لعدد معين N باستخدام a افعل...أثناء التكرار :
#include int main() { int N; printf('Enter a number to generate its multiplication table: '); scanf('%d', &N); inti = 1; do { printf('%d x %d = %d ', N, i, N * i); i++; } while (i<= 10); return 0; } < pre> <p> <strong>Output:</strong> </p> <p>Let us say you enter the number 7 as input:</p> <pre> Please enter a number to generate its multiplication table: 7 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 </pre> <p>The program calculates and prints the multiplication table for <strong> <em>7</em> </strong> from 1 to 10.</p> <h3>Infinite do while loop</h3> <p>An <strong> <em>infinite loop</em> </strong> is a loop that runs indefinitely as its condition is always <strong> <em>true</em> </strong> or it lacks a terminating condition. Here is an example of an <strong> <em>infinite do...while loop</em> </strong> in C:</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { inti = 1; do { printf('Iteration %d ', i); i++; } while (1); // Condition is always true return 0; } </pre> <p>In this <strong> <em>example</em> </strong> , the <strong> <em>loop</em> </strong> will keep running <strong> <em>indefinitely</em> </strong> because <strong> <em>condition 1</em> </strong> is always <strong> <em>true</em> </strong> .</p> <p> <strong>Output:</strong> </p> <p>When you run the program, you will see that it continues printing <strong> <em>'Iteration x',</em> </strong> where x is the <strong> <em>iteration number</em> </strong> without stopping:</p> <pre> Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on) </pre> <p>To interrupt an infinite loop like this, you generally use a <strong> <em>break statement</em> </strong> within the <strong> <em>loop</em> </strong> or some external condition you can control, such as <strong> <em>hitting</em> </strong> a specific key combination. In most desktop settings, the keyboard shortcut <strong> <em>Ctrl+C</em> </strong> can escape the Loop.</p> <h3>Nested do while loop in C</h3> <p>In C, we take an example of a <strong> <em>nested do...while loop</em> </strong> . In this example, we will write a program that uses <strong> <em>nested do...while loops</em> </strong> to create a numerical pattern.</p> <p> <strong>Example:</strong> </p> <pre> #include int main() { int rows, i = 1; printf('Enter the number of rows: '); scanf('%d', &rows); do { int j = 1; do { printf('%d ', j); j++; } while (j <= i); printf(\' \'); i++; } while (i<="rows);" return 0; < pre> <p>In this program, we use <strong> <em>nested do...while loops</em> </strong> to generate a pattern of numbers. The <strong> <em>outer loop</em> </strong> controls the number of rows, and the <strong> <em>inner loop</em> </strong> generates the numbers for each row.</p> <p> <strong>Output:</strong> </p> <p>Let us say you input five as the number of rows:</p> <pre> Enter the number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, the program generates a pattern of numbers in a <strong> <em>triangular shape</em> </strong> . The <strong> <em>outer loop</em> </strong> iterates over the rows, and the <strong> <em>inner loop</em> </strong> iterates within each row, printing the numbers from 1 up to the current row number.</p> <h2>Difference between while and do while Loop</h2> <p>Here is a tabular comparison between the while loop and the do-while Loop in C:</p> <table class="table"> <tr> <th>Aspect</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td> <strong>Syntax</strong> </td> <td>while (condition) { ... }</td> <td>do { ... } while (condition);</td> </tr> <tr> <td> <strong>Loop Body Execution</strong> </td> <td>Condition is checked before execution.</td> <td>The body is executed before the condition.</td> </tr> <tr> <td> <strong>First Execution</strong> </td> <td>The condition must be true initially.</td> <td>The body is executed at least once.</td> </tr> <tr> <td> <strong>Loop Execution</strong> </td> <td>May execute zero or more times.</td> <td>Will execute at least once.</td> </tr> <tr> <td> <strong>Example</strong> </td> <td>while (i<5) { printf(\'%d \', i); i++; }< td> <td>do { printf('%d ', i); i++; } while (i<5);< td> </5);<></td></5)></td></tr> <tr> <td> <strong>Common Use Cases</strong> </td> <td>When the loop may not run at all.</td> <td>When you want the loop to run at least once.</td> </tr> </table> <p> <strong>While Loop:</strong> The loop body is executed before the condition is checked. If the condition is initially <strong> <em>false</em> </strong> , the loop may not execute.</p> <p> <strong>Do-while Loop:</strong> The <strong> <em>loop body</em> </strong> is executed at least once before the condition is <strong> <em>checked</em> </strong> . This guarantees that the loop completes at least one iteration.</p> <p>When you want the <strong> <em>loop</em> </strong> to run based on a condition that may be <strong> <em>false</em> </strong> at first, use the <strong> <em>while loop</em> </strong> , and when you want the loop to run at least once regardless of the starting state, use the <strong> <em>do-while loop.</em> </strong> </p> <h2>Features of do while loop</h2> <p>The do-while loop in C has several fundamental characteristics that make it an effective programming technique in certain situations. The following are the significant characteristics of the do-while loop:</p> <ul> <tr><td>Guaranteed Execution:</td> Unlike other <strong> <em>loop structures</em> </strong> , the <strong> <em>do-while oop</em> </strong> ensures that the loop body is executed at least once. Because the condition is assessed after the loop body, the code within the loop is performed before the condition is verified. </tr><tr><td>Loop after testing:</td> The <strong> <em>do-while loop</em> </strong> is a post-tested loop which implies that the loop condition is assessed after the loop body has been executed. If the condition is true, the loop body is run once again. This behavior allows you to verify the condition for repetition before ensuring that a given activity is completed. </tr><tr><td>Conditionally Controlled:</td> The loop continues to execute as long as the condition specified after the while keyword remains <strong> <em>true</em> </strong> . When the condition evaluates to <strong> <em>false</em> </strong> , the loop is terminated, and control shifts to the sentence after the loop. </tr><tr><td>Flexibility:</td> The <strong> <em>do-while loop</em> </strong> may be utilized in several contexts. It is typically used in cases where a piece of code must be executed at least once, such as <strong> <em>menu-driven programs, input validation,</em> </strong> or <strong> <em>repetitive computations</em> </strong> . </tr><tr><td>Nesting Capability:</td> Similar to other <strong> <em>loop constructs</em> </strong> , the <strong> <em>do-while loop</em> </strong> can be <strong> <em>nested</em> </strong> inside other <strong> <em>loops</em> </strong> or <strong> <em>control structures</em> </strong> to create more complex control flow patterns. It allows for the creation of <strong> <em>nested loops</em> </strong> and the implementation of intricate repetitive tasks. </tr><tr><td>Break and Continue:</td> The break statement can be used within a <strong> <em>do-while loop</em> </strong> to terminate the loop execution and exit the loop prematurely. The <strong> <em>continue statement</em> </strong> can skip the remaining code in the current iteration and jump to the next iteration of the loop. </tr><tr><td>Local Scope:</td> Variables declared inside the <strong> <em>do-while loop</em> </strong> body have local scope and are accessible only within the <strong> <em>loop block.</em> </strong> They cannot be accessed outside the loop or by other loops or control structures. </tr><tr><td>Infinite Loop Control:</td> It is crucial to ensure that the loop's condition is eventually modified within the <strong> <em>loop body</em> </strong> . This modification is necessary to prevent infinite loops where the condition continually evaluates to true. Modifying the condition ensures that the loop terminates at some point. </tr></ul> <hr></=></pre></=>
يقوم البرنامج بحساب وطباعة جدول الضرب 7 من 1 إلى 10.
لانهائي القيام به أثناء الحلقة
ان حلقة لا نهائية هي حلقة تعمل إلى أجل غير مسمى كما هي حالتها دائمًا حقيقي أو أنه يفتقر إلى شرط الإنهاء. هنا مثال على فعل لا نهائي...أثناء الحلقة شركة:
مثال:
#include int main() { inti = 1; do { printf('Iteration %d ', i); i++; } while (1); // Condition is always true return 0; }
في هذا مثال ، ال حلقة سوف تستمر في الجري إلى أجل غير مسمى لأن الحالة 1 دائما حقيقي .
انتاج:
عند تشغيل البرنامج، سترى أنه يستمر في الطباعة 'التكرار س'، حيث x هو رقم التكرار من غير توقف:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on)
لمقاطعة حلقة لا نهائية مثل هذه، عادةً ما تستخدم ملف بيان كسر في حدود حلقة أو بعض الظروف الخارجية التي يمكنك التحكم فيها، مثل ضرب مجموعة مفاتيح محددة. في معظم إعدادات سطح المكتب، يكون اختصار لوحة المفاتيح السيطرة + C يمكن الهروب من الحلقة.
متداخلة تفعل أثناء حلقة في C
في C، نأخذ مثالا على متداخلة تفعل...أثناء الحلقة . في هذا المثال سوف نقوم بكتابة برنامج يستخدم متداخلة تفعل...بينما الحلقات لإنشاء نمط رقمي.
مثال:
#include int main() { int rows, i = 1; printf('Enter the number of rows: '); scanf('%d', &rows); do { int j = 1; do { printf('%d ', j); j++; } while (j <= i); printf(\' \'); i++; } while (i<="rows);" return 0; < pre> <p>In this program, we use <strong> <em>nested do...while loops</em> </strong> to generate a pattern of numbers. The <strong> <em>outer loop</em> </strong> controls the number of rows, and the <strong> <em>inner loop</em> </strong> generates the numbers for each row.</p> <p> <strong>Output:</strong> </p> <p>Let us say you input five as the number of rows:</p> <pre> Enter the number of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, the program generates a pattern of numbers in a <strong> <em>triangular shape</em> </strong> . The <strong> <em>outer loop</em> </strong> iterates over the rows, and the <strong> <em>inner loop</em> </strong> iterates within each row, printing the numbers from 1 up to the current row number.</p> <h2>Difference between while and do while Loop</h2> <p>Here is a tabular comparison between the while loop and the do-while Loop in C:</p> <table class="table"> <tr> <th>Aspect</th> <th>while loop</th> <th>do-while loop</th> </tr> <tr> <td> <strong>Syntax</strong> </td> <td>while (condition) { ... }</td> <td>do { ... } while (condition);</td> </tr> <tr> <td> <strong>Loop Body Execution</strong> </td> <td>Condition is checked before execution.</td> <td>The body is executed before the condition.</td> </tr> <tr> <td> <strong>First Execution</strong> </td> <td>The condition must be true initially.</td> <td>The body is executed at least once.</td> </tr> <tr> <td> <strong>Loop Execution</strong> </td> <td>May execute zero or more times.</td> <td>Will execute at least once.</td> </tr> <tr> <td> <strong>Example</strong> </td> <td>while (i<5) { printf(\'%d \', i); i++; }< td> <td>do { printf('%d ', i); i++; } while (i<5);< td> </5);<></td></5)></td></tr> <tr> <td> <strong>Common Use Cases</strong> </td> <td>When the loop may not run at all.</td> <td>When you want the loop to run at least once.</td> </tr> </table> <p> <strong>While Loop:</strong> The loop body is executed before the condition is checked. If the condition is initially <strong> <em>false</em> </strong> , the loop may not execute.</p> <p> <strong>Do-while Loop:</strong> The <strong> <em>loop body</em> </strong> is executed at least once before the condition is <strong> <em>checked</em> </strong> . This guarantees that the loop completes at least one iteration.</p> <p>When you want the <strong> <em>loop</em> </strong> to run based on a condition that may be <strong> <em>false</em> </strong> at first, use the <strong> <em>while loop</em> </strong> , and when you want the loop to run at least once regardless of the starting state, use the <strong> <em>do-while loop.</em> </strong> </p> <h2>Features of do while loop</h2> <p>The do-while loop in C has several fundamental characteristics that make it an effective programming technique in certain situations. The following are the significant characteristics of the do-while loop:</p> <ul> <tr><td>Guaranteed Execution:</td> Unlike other <strong> <em>loop structures</em> </strong> , the <strong> <em>do-while oop</em> </strong> ensures that the loop body is executed at least once. Because the condition is assessed after the loop body, the code within the loop is performed before the condition is verified. </tr><tr><td>Loop after testing:</td> The <strong> <em>do-while loop</em> </strong> is a post-tested loop which implies that the loop condition is assessed after the loop body has been executed. If the condition is true, the loop body is run once again. This behavior allows you to verify the condition for repetition before ensuring that a given activity is completed. </tr><tr><td>Conditionally Controlled:</td> The loop continues to execute as long as the condition specified after the while keyword remains <strong> <em>true</em> </strong> . When the condition evaluates to <strong> <em>false</em> </strong> , the loop is terminated, and control shifts to the sentence after the loop. </tr><tr><td>Flexibility:</td> The <strong> <em>do-while loop</em> </strong> may be utilized in several contexts. It is typically used in cases where a piece of code must be executed at least once, such as <strong> <em>menu-driven programs, input validation,</em> </strong> or <strong> <em>repetitive computations</em> </strong> . </tr><tr><td>Nesting Capability:</td> Similar to other <strong> <em>loop constructs</em> </strong> , the <strong> <em>do-while loop</em> </strong> can be <strong> <em>nested</em> </strong> inside other <strong> <em>loops</em> </strong> or <strong> <em>control structures</em> </strong> to create more complex control flow patterns. It allows for the creation of <strong> <em>nested loops</em> </strong> and the implementation of intricate repetitive tasks. </tr><tr><td>Break and Continue:</td> The break statement can be used within a <strong> <em>do-while loop</em> </strong> to terminate the loop execution and exit the loop prematurely. The <strong> <em>continue statement</em> </strong> can skip the remaining code in the current iteration and jump to the next iteration of the loop. </tr><tr><td>Local Scope:</td> Variables declared inside the <strong> <em>do-while loop</em> </strong> body have local scope and are accessible only within the <strong> <em>loop block.</em> </strong> They cannot be accessed outside the loop or by other loops or control structures. </tr><tr><td>Infinite Loop Control:</td> It is crucial to ensure that the loop's condition is eventually modified within the <strong> <em>loop body</em> </strong> . This modification is necessary to prevent infinite loops where the condition continually evaluates to true. Modifying the condition ensures that the loop terminates at some point. </tr></ul> <hr></=>
توضيح:
في هذا المثال، يقوم البرنامج بإنشاء نمط من الأرقام في ملف شكل مثلث . ال الحلقة الخارجية يتكرر على الصفوف، و الحلقة الداخلية يتكرر داخل كل صف، ويطبع الأرقام من 1 إلى رقم الصف الحالي.
الفرق بين while و do while Loop
فيما يلي مقارنة جدولية بين حلقة while وحلقة do-while في لغة C:
وجه | حائط اللوب | افعل بينما حلقة |
---|---|---|
بناء الجملة | بينما (الشرط) { ... } | افعل { ... } بينما (الحالة) ؛ |
تنفيذ حلقة الجسم | يتم فحص الحالة قبل التنفيذ. | يتم تنفيذ الجسم قبل الشرط. |
التنفيذ الأول | يجب أن يكون الشرط صحيحا في البداية. | يتم إعدام الجسد مرة واحدة على الأقل. |
تنفيذ الحلقة | قد يتم تنفيذه صفر مرة أو أكثر. | سيتم التنفيذ مرة واحدة على الأقل. |
مثال | عندما أنا<5) { printf(\'%d \', i); i++; }< td> | افعل { printf('%d ', i); أنا++; } عندما أنا<5);< td> 5);<> | 5)>
حالات الاستخدام الشائعة | عندما لا يمكن تشغيل الحلقة على الإطلاق. | عندما تريد تشغيل الحلقة مرة واحدة على الأقل. |
حائط اللوب: يتم تنفيذ نص الحلقة قبل التحقق من الشرط. إذا كان الشرط في البداية خطأ شنيع ، قد لا يتم تنفيذ الحلقة.
حلقة 'افعل أثناء': ال هيئة الحلقة يتم تنفيذه مرة واحدة على الأقل قبل تنفيذ الشرط التحقق . وهذا يضمن أن الحلقة تكمل تكرارًا واحدًا على الأقل.
جافا نوع المتغير
عندما تريد حلقة لتشغيل على أساس الشرط الذي قد يكون خطأ شنيع في البداية، استخدم حائط اللوب ، وعندما تريد تشغيل الحلقة مرة واحدة على الأقل بغض النظر عن حالة البداية، استخدم الأمر افعل بينما حلقة.
ميزات القيام أثناء الحلقة
تتميز حلقة do-while في لغة C بالعديد من الخصائص الأساسية التي تجعلها تقنية برمجة فعالة في مواقف معينة. فيما يلي الخصائص الهامة للحلقة do-while:
=>=>=10);>=>