logo

tee while-silmukka C:ssä

A silmukka on ohjelmoinnin ohjausrakenne, jonka avulla voit suorittaa a koodilohko toistaiseksi, jos tietty ehto täyttyy. Silmukoita käytetään toistuvien toimintojen suorittamiseen ja ohjelmoinnin suorituskyvyn parantamiseen. C-ohjelmointikielessä on useita silmukoita, joista yksi on 'do-whel' -silmukka .

A 'do-whel' -silmukka on muotoa a silmukka C:ssä, joka suorittaa koodilohkon ensin ja sen jälkeen ehdon. Jos ehto on totta , silmukka jatkaa toimintaansa; muuten se pysähtyy. Kuitenkin, onko ehto alun perin totta , se varmistaa, että koodilohko suoritetaan vähintään kerran.

do while -silmukan syntaksi

C-kielen do-while-silmukan syntaksi on annettu alla:

 do{ //code to be executed }while(condition); 

Komponentit on jaettu seuraaviin:

java poikkeuksia
  • The tee avainsana merkitsee Loopin alkua.
  • The koodilohko sisällä aaltosulkeet {} on silmukan runko, joka sisältää koodin, jonka haluat toistaa.
  • The kun avainsana seuraa sulkeissa oleva ehto (). Kun koodilohko on suoritettu, tämä ehto varmistetaan. Jos ehto on totta , silmukka jatkuu muualla, silmukan päät .

Työskentely silmukan aikana C-tilassa

Katsotaanpa esimerkkiä siitä, kuinka a do-while -silmukka toimii C:ssä. Tässä esimerkissä kirjoitamme yksinkertaisen ohjelman, joka kysyy käyttäjältä a Salasana ja kysyy, kunnes oikea salasana syötetään.

Esimerkki:

 #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; } 

Ohjelma toimii seuraavasti:

  1. Mukana ovat seuraavat otsikkotiedostot: standardille syöttö ja ulostulo rutiinit ja merkkijonoa varten manipulointitoiminnot .
  2. Oikea salasana määritellään a merkkijono (merkki salasana[]) arvon kanssa 'salaisuus'
  3. Tämän jälkeen määritämme toisen merkkijonon syötteen käyttäjän syötteen tallentamiseksi.
  4. The tee avainsana osoittaa, että koodilohko sisältyy silmukka suoritetaan vähintään kerran.
  5. Käyttämällä printf()-funktio , näytämme kehotteen, jossa käyttäjää pyydetään syöttämään salasanansa silmukan sisällä.
  6. Seuraavaksi luemme käyttäjän syöttöä käyttämällä scanf()-funktio ja säilytä se syöttötaulukko .
  7. Luettuaan syöttö , käytämme strcmp()-funktio vertailla syötettä oikeaan salasanaan. Jos jouset ovat yhtä suuri, the strcmp-toiminto palauttaa 0. Jatkamme siis silmukkaa niin kauan kuin syöte ja salasana eivät ole samat.
  8. Kerran oikea salasana syötetään, silmukka päättyy ja tulostamme 'Pääsy myönnetty!' käyttämällä printf()-funktio .
  9. Tämän jälkeen ohjelma palauttaa 0:n osoittamaan onnistuneen suorituksen.

Lähtö:

Käydään läpi mahdollinen skenaario:

 Enter the password: 123 Enter the password: abc Enter the password: secret Access Granted! 

Selitys:

Tässä esimerkissä käyttäjä syöttää aluksi väärät salasanat, '123' ja 'abc' . Silmukka pyytää käyttäjää, kunnes oikea salasana 'salaisuus' on syötetty. Kun oikea salasana on annettu, silmukka päättyy ja 'Pääsy myönnetty!' viesti tulee näkyviin.

Esimerkki do while -silmukasta C:ssä:

Esimerkki 1:

Tässä on yksinkertainen esimerkki a 'do-whel' -silmukka C:ssä, joka tulostaa numerot väliltä 1-5:

 #include int main() { inti = 1; do { printf('%d
&apos;, 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(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;number); do{ printf(&apos;%d 
&apos;,(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&apos;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(&apos;Enter a number to generate its multiplication table: &apos;); scanf(&apos;%d&apos;, &amp;N); inti = 1; do { printf(&apos;%d x %d = %d
&apos;, 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(&apos;Iteration %d
&apos;, 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>&apos;Iteration x&apos;,</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(&apos;Enter the number of rows: &apos;); scanf(&apos;%d&apos;, &amp;rows); do { int j = 1; do { printf(&apos;%d &apos;, 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(&apos;%d
&apos;, 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&apos;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></=>

Selitys:

Tässä esimerkissä koodilohko do-silmukan sisällä suoritetaan vähintään kerran, tulostaen numerot kohteesta 1-5 . Jokaisen iteraation jälkeen minä arvostan kasvaa, ja ehto i<= 5< em> on tarkistettu. Jos ehto on edelleen tosi, silmukka jatkuu; muuten se päättyy.

Esimerkki 2:

Ohjelma tulostaa taulukon annetulle numerolle käyttämällä do while Loop -toimintoa

 #include intmain(){ inti=1,number=0; printf(&apos;Enter a number: &apos;); scanf(&apos;%d&apos;,&amp;number); do{ printf(&apos;%d 
&apos;,(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&apos;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(&apos;Enter a number to generate its multiplication table: &apos;); scanf(&apos;%d&apos;, &amp;N); inti = 1; do { printf(&apos;%d x %d = %d
&apos;, 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(&apos;Iteration %d
&apos;, 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>&apos;Iteration x&apos;,</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(&apos;Enter the number of rows: &apos;); scanf(&apos;%d&apos;, &amp;rows); do { int j = 1; do { printf(&apos;%d &apos;, 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(&apos;%d
&apos;, 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&apos;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);>

Esimerkki 3:

Otetaan ohjelma, joka tulostaa tietyn luvun N kertotaulukon käyttämällä a:ta tehdä... kun Loop :

 #include int main() { int N; printf(&apos;Enter a number to generate its multiplication table: &apos;); scanf(&apos;%d&apos;, &amp;N); inti = 1; do { printf(&apos;%d x %d = %d
&apos;, 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(&apos;Iteration %d
&apos;, 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>&apos;Iteration x&apos;,</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(&apos;Enter the number of rows: &apos;); scanf(&apos;%d&apos;, &amp;rows); do { int j = 1; do { printf(&apos;%d &apos;, 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(&apos;%d
&apos;, 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&apos;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></=>

Ohjelma laskee ja tulostaa kertotaulukon 7 1-10.

Infinite do while -silmukka

An ääretön silmukka on silmukka, joka kulkee loputtomiin, kuten sen ehto on aina totta tai siitä puuttuu lopetusehto. Tässä on esimerkki an infinite do...while loop C:ssa:

Esimerkki:

 #include int main() { inti = 1; do { printf(&apos;Iteration %d
&apos;, i); i++; } while (1); // Condition is always true return 0; } 

Tässä esimerkki , silmukka jatkaa juoksua toistaiseksi koska ehto 1 on aina totta .

Lähtö:

Kun suoritat ohjelman, näet, että se jatkaa tulostusta 'Iteraatio x', missä x on iterointinumero pysähtymättä:

 Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 ... (and so on) 

Jos haluat keskeyttää tällaisen äärettömän silmukan, käytät yleensä a katkeaa lausunto sisällä silmukka tai jokin ulkoinen ehto, jota voit hallita, kuten lyömällä tietty näppäinyhdistelmä. Useimmissa työpöytäasetuksissa pikanäppäin Ctrl+C voi paeta Loopista.

Sisäkkäinen do while -silmukka C:ssä

Otetaan C:ssä esimerkki a Nested do...while silmukka . Tässä esimerkissä kirjoitamme ohjelman, joka käyttää Nested do...when silmukat luodaksesi numeerisen kuvion.

Esimerkki:

 #include int main() { int rows, i = 1; printf(&apos;Enter the number of rows: &apos;); scanf(&apos;%d&apos;, &amp;rows); do { int j = 1; do { printf(&apos;%d &apos;, 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(&apos;%d
&apos;, 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&apos;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></=>

Selitys:

Tässä esimerkissä ohjelma luo numerokuvion a:ssa kolmion muotoinen . The ulkoinen silmukka iteroituu rivien yli ja sisäinen silmukka iteroituu jokaisen rivin sisällä ja tulostaa numerot 1:stä nykyiseen rivinumeroon.

Ero while- ja do while -silmukan välillä

Tässä on taulukkovertailu while-silmukan ja do-while-silmukan välillä C:ssä:

Aspekti kun silmukka do-while -silmukka
Syntaksi kun (ehto) { ... } do { ... } while (ehto);
Loop-kehon suoritus Kunto tarkistetaan ennen suoritusta. Keho teloitetaan ennen ehtoa.
Ensimmäinen teloitus Ehdon on oltava totta alun perin. Ruumis teloitetaan vähintään kerran.
Silmukan suoritus Voidaan suorittaa nolla kertaa tai useammin. Suoritetaan vähintään kerran.
Esimerkki sillä aikaa kun minä<5) { printf(\'%d \', i); i++; }< td> do { printf('%d ', i); i++; } sillä aikaa kun minä<5);< td>
Yleiset käyttötapaukset Kun silmukka ei välttämättä toimi ollenkaan. Kun haluat silmukan ajavan ainakin kerran.

Silmukan aikana: Silmukan runko suoritetaan ennen ehdon tarkistamista. Jos tila on alun perin väärä , silmukka ei välttämättä toimi.

Tee, kun silmukka: The silmukan runko suoritetaan vähintään kerran ennen ehdon täyttymistä tarkistettu . Tämä takaa, että silmukka suorittaa ainakin yhden iteraation.

java objektien tasa-arvo

Kun haluat silmukka ajaa mahdollisen ehdon perusteella väärä käytä aluksi kun silmukka , ja kun haluat silmukan ajavan vähintään kerran aloitustilasta riippumatta, käytä do-while -silmukka.

Do while -silmukan ominaisuudet

C:n do-while-silmukalla on useita perusominaisuuksia, jotka tekevät siitä tehokkaan ohjelmointitekniikan tietyissä tilanteissa. Seuraavat ovat do-while-silmukan tärkeät ominaisuudet:

    Taattu toteutus:Toisin kuin muut silmukkarakenteet , do-when oop varmistaa, että silmukan runko suoritetaan vähintään kerran. Koska tila arvioidaan silmukan rungon jälkeen, silmukan sisällä oleva koodi suoritetaan ennen ehdon tarkistamista.Silmukka testauksen jälkeen:The do-while -silmukka on jälkitestattu silmukka, joka tarkoittaa, että silmukan tila arvioidaan sen jälkeen, kun silmukan runko on suoritettu. Jos ehto on tosi, silmukan runko ajetaan uudelleen. Tämän toiminnan avulla voit tarkistaa toiston ehdon ennen kuin varmistat, että tietty toiminto on suoritettu.Ehdollisesti valvottu:Silmukan suoritus jatkuu niin kauan kuin while-avainsanan jälkeen määritetty ehto säilyy totta . Kun tila arvioi väärä , silmukka päättyy ja ohjaus siirtyy silmukan jälkeiseen lauseeseen.Joustavuus:The do-while -silmukka voidaan käyttää useissa yhteyksissä. Sitä käytetään tyypillisesti tapauksissa, joissa koodinpätkä on suoritettava vähintään kerran, kuten esim valikkopohjaiset ohjelmat, syötteiden tarkistus, tai toistuvia laskelmia .Sisäkkäisyysmahdollisuus:Samanlainen kuin muut silmukkarakenteet , do-while -silmukka voi olla sisäkkäisiä toisen sisällä silmukat tai ohjausrakenteet monimutkaisempien ohjausvirtakuvioiden luomiseen. Se mahdollistaa luomisen sisäkkäisiä silmukoita ja monimutkaisten toistuvien tehtävien toteuttaminen.Tauko ja jatka:Break-lausetta voidaan käyttää sisällä a do-while -silmukka lopettaaksesi silmukan suorituksen ja poistuaksesi silmukasta ennenaikaisesti. The jatka lausuntoa voi ohittaa jäljellä olevan koodin nykyisessä iteraatiossa ja siirtyä silmukan seuraavaan iteraatioon.Paikallinen laajuus:Muuttujat ilmoitettu sisällä do-while -silmukka elimillä on paikallinen laajuus ja ne ovat käytettävissä vain alueella silmukkalohko. Niihin ei pääse käsiksi silmukan ulkopuolella tai muilla silmukoilla tai ohjausrakenteilla.Infinite Loop Control:On erittäin tärkeää varmistaa, että silmukan tilaa muutetaan lopulta sisällä silmukan runko . Tämä muutos on välttämätön, jotta estetään äärettömät silmukat, joissa ehto muuttuu jatkuvasti todeksi. Ehdon muokkaaminen varmistaa, että silmukka päättyy jossain vaiheessa.