Computer Science

Java Do While Loop

The Java do-while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block as long as a specified condition is true. It is similar to the while loop, but the condition is checked after the block of code is executed, ensuring that the block is executed at least once.

Written by Perlego with AI-assistance

3 Key excerpts on "Java Do While Loop"

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.
  • HTML, CSS & JavaScript in easy steps

    ...Statements to be executed on each iteration are enclosed within braces along with a statement that modifies a value in the test condition, so that at some point its evaluation will return false and the loop will exit. While the evaluation remains true, the statements will be executed on each iteration of the loop. Where the condition evaluation is false on the first iteration, the loop exits immediately so the statements within its braces have been executed once. The do while loop is sometimes referred to as a “post-test” loop because the test condition is evaluated after its statements have been executed. A do while loop can be made to perform a specific number of iterations, like a for loop, by using a counter variable as the test condition and incrementing its value on each iteration. For example, a do while loop structure to execute a set of statements 100 times might look like this: let i = 0 do { statements-to-be-executed i++ } while (i < 100) The counter variable is incremented on each iteration until its value reaches 100, upon which the evaluation returns false and the loop ends. Only use a do while loop if the statements absolutely must be executed at least once. Create an HTML document with a self-invoking function that begins by initializing a loop counter variable let i = 2 do.html Next, insert a do while loop structure that will make iterations and output the value of the loop counter on each iteration until it exceeds 1000 do { i *= 2 console.log(‘ Multiplied Number: ‘ + i) } while(i < 1000) Save the HTML document, then open it in your browser and launch the console to see the loop iterations Notice that the final value exceeds the condition limit because it gets written in output before the test is made. Break Out The. JavaScript break keyword can be used to exit from a loop when a specified condition is encountered...

  • C# 2010 All-in-One For Dummies
    • Bill Sempf, Charles Sphar, Stephen R. Davis(Authors)
    • 2010(Publication Date)
    • For Dummies
      (Publisher)

    ...Usually, this means your counting variable is being incremented properly. Otherwise, you’re looking at an infinite loop, an angry user, bad press, and 50 years of drought. Infinite loops are a common mistake, so don’t be embarrassed when you get caught in one. Doing the do. . . while loop A variation of the while loop is the do. . . while loop. In this example, the condition isn’t checked until the end of the loop: int year = 1; do { //. . . some calculation. . . year = year + 1; } while (year < duration); In contrast to the while loop, the do. . . while loop is executed at least once, regardless of the value of duration. Breaking up is easy to do You can use two special commands to bail out of a loop: break and continue. Executing the break command causes control to pass to the first expression immediately following the loop. The similar continue command passes control straight back up to the conditional expression at the top of the loop to start over and get it right this time. I have rarely used continue in my programming career, and I doubt that many programmers even remember that it exists. Don’t forget about it completely because it may be a trick question in an interview or a crossword puzzle. Suppose that you want to take your money out of the bank as soon as the principal exceeds a certain number of times the original amount, irrespective of the duration in years. (After all, how much money do you really need?) You could easily accommodate this amount by adding the following code within the loop: if (principal > (maxPower * originalPrincipal)) { break; } Anyone who watches The Simpsons as much as I do knows who maxPower is. (Hint: D’oh!) The break clause isn’t executed until the condition within the if comparison is true — in this case, until the calculated principal is maxPower times the original principal or more...

  • 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)...