JavaScript basics
JavaScript for loop writing
JavaScript for loop (for Loop) can be used to execute a piece of code. When the variable value matches the set condition, it will continue to execute until the condition is not met before stopping execution. The scope of application is similar to JavaScript while loop, but the writing method is as follows A big difference.
JavaScript for loop basic architecture
for (variable = initial value; variable <limit value; variable + step value) {
code to be executed
}
The first line is the most important part of the for loop. The variables , initial value , limit value and step value are specified here . When the first loop of javascript for loop is executed, the variable will run a loop with the initial value. In the second lap, the step value is added to the variable first, and then it is compared whether the new variable value is smaller than the limit value. If it is, the for loop continues until the variable value equals or exceeds the limit value. At that time, the JavaScript for loop will stop.code to be executed
}
JavaScript for loop example
<script language="javascript">
for(i=0;i<5;i++){
document.write(i);
}
</script>
Value line result: 01234for(i=0;i<5;i++){
document.write(i);
}
</script>
Since the initial value of the variable i we set is 0, and the limit value is 5, i++ means +1 every time the loop is completed, so this loop starts from 0 and always displays Until 4, 5 will not be displayed. When writing loops, check as much as possible before testing to avoid infinite loops and browser crashes.
Post a Comment
0 Comments