Computer Science

Do While Loop in C

The "Do While Loop in C" is a control flow statement that allows a specific block of code to be executed repeatedly as long as a specified condition is true. It differs from the "While Loop" in that it guarantees the execution of the block of code at least once before checking the condition. This loop is commonly used for tasks that require iteration until a certain condition is met.

Written by Perlego with AI-assistance

3 Key excerpts on "Do While Loop in C"

Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.
  • Using LEDs, LCDs and GLCDs in Microcontroller Projects
    • Dogan Ibrahim(Author)
    • 2012(Publication Date)
    • Wiley
      (Publisher)

    ...On the other hand, the statements inside the do – while loop are executed at least once since the condition is tested at the end of the construct. Some examples are given below, showing how the do – while loop can be constructed and used in programs: Example 3.4 Write a program to initialise an integer array called MyArray to 1. Assume that the array has 100 elements. Solution 3.4 The required program is given below: Example 3.5 Write the loop code using do – while statement to copy a string called B to another string called A. Solution 3.5 Remember that strings are character arrays terminated with a NULL character. The do – while loop below will copy the contents of string B to A, including the NULL terminator. Note that i is incremented after its value has been used as a subscript to B : When using the do – while statement, it is important to make sure that the condition that holds the loop is changed inside the loop, otherwise an infinite loop is formed. The following is the above program written in error that never terminates. The program repeatedly initialises element 0 of the array: Sometimes it may be necessary to create infinite loops. This can easily be achieved using the do – while statements, as shown below: Example 3.6 Write a program to multiply two integer arrays X and Y having 10 elements each, and store the sum in an integer variable called Sum. Solution 3.6 The required program is given below: 3.2.18.6 for Statements The for statement is the most commonly used statement for setting up loops in programs. The general format of this statement is: where expression 1 sets the initial value of the loop count and this variable is evaluated just once on entry into the loop. Expression 2 is the condition that keeps the loop continuing...

  • Coding + Math
    eBook - ePub

    Coding + Math

    Strengthen K–5 Math Skills With Computer Science

    ...They harness the ever-increasing power of computers to accomplish tasks far more efficiently. Programmers need to understand loops not only as a concept, but within the contexts that they will be expected to use them. Figure 4.25 depicts the same loop program in four different potential learning contexts, each with a different level of detail and abstractness. While each approach differs in level of complexity, none appear to be beyond the reach of some students along the K–5 continuum of learning. Although students will differ on their readiness for one option over another, it is important that they are not limited by a lack of choices to pursue levels of complexity commensurate with their abilities. Figure 4.25 Comparison of While Loop program across different tools. RESOURCES RESOURCE SOURCE LINK Flowgorithm Flowcharting Program Sacramento State University (flowgorithm.org) Online Flowchart Designer JGraph Ltd. (draw.io) SmartDraw (Online Flowchart Maker) SmartDraw Software, LLC (smartdraw.com/flowchart/flowchart-maker.htm) Coding: Loops Vocabulary Activity Flocabulary (flocabulary.com/lesson/coding-for-loops) Using While Lloops Khan Academy (http://bit.ly/3cMKD8z)...

  • Hack Audio
    eBook - ePub

    Hack Audio

    An Introduction to Computer Programming and Digital Signal Processing in MATLAB

    • Eric Tarr(Author)
    • 2018(Publication Date)
    • Routledge
      (Publisher)

    ...The while loop needs to be concluded by the keyword end to indicate the lines of code to be repeated. Only the commands on lines between while and end are repeated for the loop. When execution reaches the declaration of the loop, if the logical operation is TRUE, then the code in the loop is executed. When the commands within the loop are complete, execution returns to the logical expression at the loop declaration, and it is evaluated again. As long as the logical expression remains TRUE, the loop continues to repeat. At any point, if the logical expression produces a FALSE result, then execution jumps to the end of the loop. The following is a conventional approach for using a while loop. For an existing variable in code, the loop is declared using the while keyword followed by a logical expression containing the variable. Any commands to be repeated in a given task are executed within the loop. Also within the loop, the variable used in the original logical expression is changed in some way (e.g., iterative counting), such that the logical expression can eventually change from TRUE to FALSE, so the loop exits. Several examples of this approach are demonstrated in Example 5.11. Examples: Save and run the following m-file in...