The `for` Loop
One of the most popular loops, which is available in almost every programming language, is known as the for loop. The for loop is primarily used for executing a block of code a specific number of times.
The general syntax of the for loop is as follows:
for(initialization; condition; operation) {
// code to be executed
}
The initialization part is where we declare & initialize a variable, this part is executed only once - at the start of the loop. The boolean expression in place of the condition defines the loop condition - the loop executes as long as that condition is true. The operation can be any expression which is evaluated or executed after every successful iteration.
The flow of a for loop can be better understood through a flowchart:
Following is an example of a simple for loop which executes a block of code 15 times:
123for(let i = 0; i < 15; i++) { console.log("This is the " + i + "th iteration"); }
1. What is the primary purpose of a for loop?
2. In the for loop syntax, which part is executed only once?
3. What will be the output of the following code?
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Incrível!
Completion taxa melhorada para 1.33
The `for` Loop
Deslize para mostrar o menu
One of the most popular loops, which is available in almost every programming language, is known as the for loop. The for loop is primarily used for executing a block of code a specific number of times.
The general syntax of the for loop is as follows:
for(initialization; condition; operation) {
// code to be executed
}
The initialization part is where we declare & initialize a variable, this part is executed only once - at the start of the loop. The boolean expression in place of the condition defines the loop condition - the loop executes as long as that condition is true. The operation can be any expression which is evaluated or executed after every successful iteration.
The flow of a for loop can be better understood through a flowchart:
Following is an example of a simple for loop which executes a block of code 15 times:
123for(let i = 0; i < 15; i++) { console.log("This is the " + i + "th iteration"); }
1. What is the primary purpose of a for loop?
2. In the for loop syntax, which part is executed only once?
3. What will be the output of the following code?
Obrigado pelo seu feedback!