Computer Science

Java While Loop

A Java while loop is a control flow statement that allows a block of code to be executed repeatedly based on a given boolean condition. The loop continues to execute as long as the condition remains true. It is a fundamental construct in programming for creating iterative processes and is commonly used for tasks such as iterating through arrays or performing calculations until a certain condition is met.

Written by Perlego with AI-assistance

5 Key excerpts on "Java 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.
  • 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)...

  • HTML, CSS & JavaScript in easy steps

    ...regular for loop. There is also a special for in loop that is used to iterate through properties of an object and is demonstrated later, here. Loop While The for loop structure, described here, is ideal when the number of required iterations is a known quantity, but when this is unknown a while loop structure is often preferable. The syntax of a while loop looks like this: while(condition) { statements-to-be-executed modifier } The parentheses after the while keyword contain a condition that is evaluated for a boolean value upon each iteration. 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 are never executed. Both while loops and for loops are sometimes referred to as “pre-test” loops because their test condition is evaluated before any statements are executed. A 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...

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

    ...Performing this simple interest calculation by using a calculator (or by hand, using a piece of paper) would be much easier than writing and executing a program. If you could calculate the amount of principal for each of several succeeding years, that would even more useful. A simple macro in a Microsoft Excel spreadsheet is still easier to handle, but at least you’re getting closer. What you need is a way for the computer to execute the same short sequence of instructions multiple times — known as a loop. Looping for a while The C# keyword while introduces the most basic form of execution loop: while(bool-expression) { //. . . repeatedly executed as long as the expression is true. } When the while loop is first encountered, the bool expression is evaluated. If the expression is true, the code within the block is executed. When the block of code reaches the closed brace, control returns to the top and the whole process starts over again. (It’s kind of the way I feel when I’m walking the dog. The dog and I loop around and around the yard until the dog. . . well, until he’s finished.) Control passes beyond the closed brace the first time the bool expression is evaluated and turns out to be false. If the condition is not true the first time the while loop is encountered, the set of commands within the braces is never executed. Programmers often become sloppy in their speech. (Programmers are sloppy most of the time.) If a programmer says that a loop is executed until a condition is false, it implies that control passes outside the loop — no matter where the program happens to be executing — as soon as the condition becomes false. This definitely isn’t the case...

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

  • TradeStation Made Easy!
    eBook - ePub

    TradeStation Made Easy!

    Using EasyLanguage to Build Profits with the World's Most Popular Trading Software

    • Sunny J. Harris(Author)
    • 2011(Publication Date)
    • Wiley
      (Publisher)

    ...Chapter 11 Loops In This Chapter Introduction For While INTRODUCTION A loop is just as it sounds; it goes around and around until you tell it to stop. A loop defines a group of instructions that will continue executing until a condition is met. The condition being met will cause the loop to stop. A FOR loop will execute a predefined number of times. The predefined number of times is the condition. A WHILE loop will continue to execute while (as long as) the condition is true. Loops allow you to calculate something over a range of values, such as from 1 to 10. Or to take action only while a condition is met, such as: while it is the month of October, only take short trades. Loops allow you to calculate averages, and summations and even count backward. As your programming skills become more proficient, you will find yourself discovering more and more uses for loops. FOR TEMPLATE 1 FOR Value1 = m TO n BEGIN … END; TEMPLATE 2 FOR Value1 = m DOWNTO n BEGIN … END; The FOR command always appears with a BEGIN … END block and is sometimes referred to as a FOR … BEGIN command. The FOR command is always followed by a set of variables that define the starting and ending number of steps to execute the loop. This structure is pretty complex until you get accustomed to it; then it will become second nature, so don’t start worrying yet. Templates for these concepts are shown above. You will specify m and n, and provide code for your task in place of the... s. FOR is always followed by a variable name (Value1 in Figure 11.1). The command tells the computer to perform a set of actions a specified number of times. The variable counts the number of times as it steps through the loop...