Computer Science
Javascript For Loop
A JavaScript for loop is a control flow statement that allows you to repeatedly execute a block of code. It consists of three parts: initialization, condition, and iteration, which are used to control the number of times the loop runs. This looping structure is commonly used for iterating over arrays, performing calculations, and executing code based on specific conditions.
Written by Perlego with AI-assistance
Related key terms
1 of 5
4 Key excerpts on "Javascript For Loop"
- eBook - PDF
- Rob Larsen(Author)
- 2013(Publication Date)
- Wrox(Publisher)
for The for statement executes a block of code a specified number of times. You use it when you want to specify how many times you want the code to be executed (rather than running while a particu- lar condition is true/false). It is worth noting here that the number of times that the for loop runs could be specified by some other part of the code. First, here is the syntax (which always takes three arguments): for ( a; b; c ){ //code to be executed } 362 ❘ CHAPTER 10 LEARNING JAVASCRIPT Now you need to look at what a, b, and c represent: ‰ ‰ a is evaluated before the loop is run and is only evaluated once. It is ideal for assigning a value to a variable; for example, you might use it to set a counter to 0 using i=0. ‰ ‰ b should be a condition that indicates whether the loop should be run again; if it returns true the loop runs again. For example, you might use this to check whether the counter is less than 11. ‰ ‰ c is evaluated after the loop has run and can contain multiple expressions separated by a comma (for example, i++, j++;). For example, you might use it to increment the counter. So if you come back to the 3 times table example again, it would be written something like this ( ch10_eg09.js): for ( var i=0; i<11; i++ ) { document.getElementById( "numbers" ).innerHTML += "- " + i + " x 3 = " + (i * 3) +"
"; } Now look at the for statement in small chunks: ‰ ‰ var i=0 The counter is assigned to have a value of 0. ‰ ‰ i<11 The loop should run if the value of the counter is less than 11. ‰ ‰ i++ The counter is incremented by 1 every time the loop runs. The assignment of the counter variable, the condition, and the incrementing of the counter all appear in the parentheses after the keyword for. You can also assign several variables at once in the part corresponding to the letter a if you separate them with a comma, for example, var i = 0, j = 5;. It is also worth noting that you can count downward, as well as upward, with loops. - eBook - PDF
- Patrick Carey, Sasha Vodnik, Patrick Carey(Authors)
- 2021(Publication Date)
- Cengage Learning EMEA(Publisher)
Loop statements in JavaScript include the while, do while, and for statements. ❯ ❯ The while statement is used for repeating a statement or series of statements as long as a given conditional expression evaluates to true. ❯ ❯ Each repetition of a looping statement is called an iteration. ❯ ❯ An infinite loop is a situation in which a loop statement never ends because its conditional expression is never false. ❯ ❯ The do while statement executes a statement or statements once, and then it repeats the execution as long as a given conditional expression evaluates to true. ❯ ❯ The for statement is used to repeat a statement or series of statements as long as a given conditional expression evaluates to true. ❯ ❯ The continue statement halts a looping statement and restarts the loop with a new iteration. ❯ ❯ The process of choosing which code to execute at a given point in an application is known as decision making. In JavaScript, you use the if, if else, else if, and switch statements to create decision-making structures. ❯ ❯ The if statement is used to execute specific programming code if the evaluation of a conditional expression returns a value of true. ❯ ❯ An if statement that includes an else clause is called an if else statement. ❯ ❯ When one decision-making statement is contained within another decision-making statement, they are referred to as nested decision-making structures. ❯ ❯ The switch statement controls program flow by executing a specific set of statements, depending on the value of an expression. ❯ ❯ A break statement is used to exit control statements, such as the switch statement or the while, do while, and for looping statements. - eBook - PDF
- Joyce Farrell(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
It is easy for others to read, and because the loop control variable’s initialization, testing, and alteration are all performed in one location, you are less likely to leave out one of these crucial elements. Although for loops are commonly used to control execution of a block of statements a fixed number of times, the programmer doesn’t need to know the starting, final, or step value for the loop control variable when the program is written. For example, any of the values might be entered by the user, or might be the result of a calculation. Figure 5-15 Comparable while and for statements that each output Hello four times count = 0 while count <= 3 output "Hello" count = count + 1 endwhile for count = 0 to 3 step 1 output "Hello" endfor The for loop is particularly useful when processing arrays. You will learn about arrays in Chapter 6. In Java, C11, and C#, a for loop that displays 21 values (0 through 20) might look similar to the following: for(count 5 0; count <= 20; count11) { output count; } The three actions (initializing, evaluating, and altering the loop control variable) are separated by semi- colons within a set of parentheses that follow the keyword for. The expression count11 increases count by 1. In each of the three languages, the block of statements that depends on the loop sits between a pair of curly braces, so the endfor keyword is not used. None of the three languages uses the keyword output, but all of them end output statements with a semicolon. 200 C H A P T E R 5 Looping Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Both the while loop and the for loop are examples of pretest loops. In a pretest loop, the loop control variable is tested before each iteration. That means the loop body might never execute because the evaluation controlling the loop might be false the first time it is made. - eBook - PDF
- Joyce Farrell(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
It is easy for others to read, and because the loop control variable’s initialization, testing, and alteration are all performed in one location, you are less likely to leave out one of these crucial elements. Although for loops are commonly used to control execution of a block of statements a fixed number of times, the programmer doesn’t need to know the starting, final, or step value for the loop control variable when the program is written. For example, any of the values might be entered by the user, or might be the result of a calculation. Figure 5-15 Comparable while and for statements that each output Hello four times count = 0 while count <= 3 output Hello count = count + 1 endwhile for count = 0 to 3 step 1 output Hello endfor The for loop is particularly useful when processing arrays. You will learn about arrays in Chapter 6. In Java, C 11 , and C#, a for loop that displays 21 values (0 through 20) might look similar to the following: for(count 5 0; count <= 20; count 11 ) { output count; } The three actions (initializing, evaluating, and altering the loop control variable) are separated by semi-colons within a set of parentheses that follow the keyword for . The expression count 11 increases count by 1. In each of the three languages, the block of statements that depends on the loop sits between a pair of curly braces, so the endfor keyword is not used. None of the three languages uses the keyword output , but all of them end output statements with a semicolon. 200 C H A P T E R 5 Looping Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 Both the while loop and the for loop are examples of pretest loops. In a pretest loop , the loop control variable is tested before each iteration. That means the loop body might never execute because the evaluation controlling the loop might be false the first time it is made.
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.



