Computer Science
Javascript Do While Loop
The JavaScript do-while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block of code as long as the specified condition is true. It is similar to the while loop, but the block of code is executed before the condition is checked.
Written by Perlego with AI-assistance
Related key terms
1 of 5
6 Key excerpts on "Javascript Do While Loop"
- eBook - ePub
JavaScript from Beginner to Professional
Learn JavaScript quickly by building fun, interactive, and dynamic web apps, games, and pages
- Laurence Lars Svekis, Maaike van Putten, Rob Percival, Laurence Svekis, Codestars By Rob Percival(Authors)
- 2021(Publication Date)
- Packt Publishing(Publisher)
5
Loops
We are starting to get a good basic grasp of JavaScript. This chapter will focus on a very important control flow concept: loops. Loops execute a code block a certain number of times. We can use loops to do many things, such as repeating operations a number of times and iterating over data sets, arrays, and objects. Whenever you feel the need to copy a little piece of code and place it right underneath where you copied it from, you should probably be using a loop instead.We will first discuss the basics of loops, then continue to discuss nesting loops, which is basically using loops inside loops. Also, we will explain looping over two complex constructs we have seen, arrays and objects. And finally, we will introduce two keywords related to loops, break and continue , to control the flow of the loop even more.These are the different loops we will be discussing in this chapter:There is one topic that is closely related to loops that is not in this chapter. This is the built-in foreach method. We can use this method to loop over arrays, when we can use an arrow function. Since we won't discuss these until the next chapter, foreach is not included here.- while loop
- do while loop
- for loop
- for in
- for of loop
Note: exercise, project, and self-check quiz answers can be found in the Appendix .while loops
The first loop we will discuss is the while loop . A while loop executes a certain block of code as long as an expression evaluates to true . The snippet below demonstrates the syntax of the while - eBook - ePub
Processing
An Introduction to Programming
- Jeffrey L. Nyhoff, Larry R. Nyhoff(Authors)
- 2017(Publication Date)
- Chapman and Hall/CRC(Publisher)
5Repetition with a Loop: The while StatementWe have seen in the programs we have written that the order in which the statements are written makes a difference. This illustrates a key principle of computing known as sequence .In Chapter 4, we saw that statements placed inside an if or switch statement are only performed if a certain condition is true. Such conditional programming illustrates a second key principle of computing known as selection .In this chapter and the next, we will see that, sometimes, one or more statements need to be performed repeatedly in a computer program. This will illustrate a third key principle in computing: repetition .Human beings often find repetitive tasks to be tedious. In contrast, computers can provide an ideal way to perform repetitive tasks. Repetition as Long as a Certain Condition Is TrueLike the if statement, each repetition structure in this chapter will make use of a condition . This condition will determine whether the repetition should continue .Once again, let’ s consider a cooking recipe as an analogy to a computer program. Suppose a certain dough recipe says that you are to mix the ingredients and then repeatedly add ½ cup of flour as long as you can still stir the mixture with a spoon. A flowchart of this recipe might look like the following:Similarly, in computer programming, there are sometimes sets of statements that we would like to perform repeatedly , but only as long as a certain condition is true.Repetition with the while StatementProcessing provides several programming structures that can be used for creating repetition. One of the more versatile of these repetition structures is the while statement. The basic form of the while - eBook - ePub
- Andy Harris(Author)
- 2014(Publication Date)
- For Dummies(Publisher)
endless loops are especially troubling in JavaScript because they can crash the entire browser. If a JavaScript program gets into an endless loop, often the only solution is to use the operating system task manager (Ctrl+Alt+Delete on Windows) to shut down the entire browser.The easy way to make sure your loop works is to remember that while loops need all the same features as for loops. (These ideas are built into the structure of a for loop. You're responsible for them yourself in a while loop.) If your loop doesn't work, check that you've followed these steps:- Identify a key variable: A while loop is normally based on a condition, which is usually a comparison (although it might also be a variable or function that returns a Boolean value). In a for loop, the key variable is almost always an integer. While loops can be based on any type of variable.
- Initialize the variable before the loop: Before the loop begins, set up the initial value of the key variable to ensure the loop happens at least once. (How does the variable start?)
- Identify the condition for the loop: A while loop is based on a condition. Define the condition so the loop continues while the condition is true , and exits when the condition is evaluated to false . (How does the variable end?)
- Change the condition inside the loop: Somewhere inside the loop code, you need to have statements that will eventually make the condition false . If you forget this part, your loop will never end. (How does the variable change?)
This example is a good example of a while loop, but a terrible way to handle security. The password is shown in the clear, and anybody could view the source code to see the correct password. There are far better ways to handle security, but this is the cleanest example of a while - eBook - PDF
- Joyce Farrell(Author)
- 2018(Publication Date)
- Cengage Learning EMEA(Publisher)
C H A P T E R 6 Looping Upon completion of this chapter, you will be able to: Describe the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops Nest loops Improve loop performance Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 284 Looping C H A P T E R 6 Learning About the Loop Structure If making decisions is what makes programs seem smart, looping is what makes programs seem powerful. A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated. If it is true, a block of statements called the loop body executes and the Boolean expression is evaluated again. The loop body can be a single statement, or a block of statements between curly braces. As long as the loop-controlling, Boolean expression is true, the statements in the loop body continue to execute. When the Boolean evaluation is false, the loop ends. One execution of any loop is called an iteration. Figure 6-1 shows a diagram of the logic of a loop. In Java, you can use several mechanisms to create loops. In this chapter, you learn to use three types of loops: • A while loop, in which the loop-controlling Boolean expression is the first statement in the loop, evaluated before the loop body ever executes • A for loop, which is usually used as a concise format in which to execute loops • A do…while loop, in which the loop- controlling Boolean expression is the last statement in the loop, evaluated after the loop body executes one time. - eBook - ePub
- Kuldeep Singh Kaswan, Jagjit Singh Dhatterwal, B Balamurugan(Authors)
- 2023(Publication Date)
- Chapman and Hall/CRC(Publisher)
5 Iterative Control StructureDOI: 10.1201/9781003202035-5Looping is a program or script procedure that performs repetitive (or iterative) tasks. The loop is a sequence of instructions (a block) that is executed repeatedly while or until some condition is met or satisfied. Each repetition is called an iteration. All programming and scripting languages provide loop structures, and, like other languages, Python has several looping constructs [28 ].Algorithm design is frequently based on iteration and conditional execution.5.1 The While Loop
The output of Program 5.1 (counting.py) counts up to five.print (5) print (4) print (3) print (2) print (1)Output 5 4 3 2 1How would you write the code to count to 10,000? Would you copy, paste, and modify 10,000 printing statements? You could, but that would be impractical! Counting is such a common activity, and computers routinely count up to very large values, that there must be a better way. What we would really like to do is print the value of a variable (call it count), then increment the variable (count += 1), repeating this process until the variable is large enough (count == 5 or maybe count == 10000). Python has two different keywords, while and for , that enable iteration [29 ].Program 5.2 (loopcounting.py) uses the keyword while to count up to five:count = 1 while count <= 5: print (count) count += 1Output 1 2 3 4 5The while keyword in Program 5.2 (loopcounting.py) causes the variable ‘count’ to be repeatedly printed then incremented until its value reaches five. The block of statementsprint(count) count += 1is executed five times. After each print of the variable count - eBook - PDF
Microsoft® Visual C# 2015
An Introduction to Object-Oriented Programming
- Joyce Farrell, , , (Authors)
- 2015(Publication Date)
- Cengage Learning EMEA(Publisher)
Within a looping structure, a Boolean expression is evaluated. If it is true , a block of statements called the loop body executes and the Boolean expression is evaluated again. As long as the expression is true , the statements in the loop body continue to execute and the loop-controlling Boolean expression continues to be reevaluated. When the Boolean evaluation is false , the loop ends. Figure 5-1 shows a diagram of the logic of a loop. One execution of any loop is called an iteration . %RROHDQ H[SUHVVLRQ WUXH IDOVH /RRS ERG Figure 5-1 Flowchart of a loop structure You can use a while loop to execute a body of statements continuously as long as the loop’s test condition continues to be true . A while loop consists of the following: • the keyword while • a Boolean expression within parentheses • the loop body The evaluated Boolean expression in a while statement can be either a single Boolean expression or a compound expression that uses ANDs and ORs. The body can be a single statement or any number of statements surrounded by curly braces. Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 191 Creating Loops with the while Statement For example, the following code shows an integer declaration followed by a loop that causes the message Hello to display (theoretically) forever because there is no code to end the loop. A loop that never ends is called an infinite loop . An infinite loop might not actually execute infinitely.
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.





