Break and continue statements provide stricter process control over code execution in loops. The break statement can exit the loop immediately, preventing any code in the loop body from being executed again. The continue statement just exits the current loop, and according to the control expression, the next loop is also allowed.
break
The code copy is as follows:
<script language="javascript">
var aNumbers = new Array();
var sMessage = "You entered:<br>";
var iTotal = 0;
var vUserInput;
var iArrayIndex = 0;
do{
vUserInput = Number(prompt("Enter a number, or '0'Exit","0"));
if(isNaN(vUserInput)){
document.write("Input error, please enter the number, '0' exit<br>");
break; //Enter errors directly exit the entire do loop body
}
aNumbers[iArrayIndex] = vUserInput;
iArrayIndex++;
}while(vUserInput != 0) // Exit the loop body when the input is 0 (default value).
// Common methods for looping through arrays:
for(var i=0;i<aNumbers.length;i++){
iTotal += Number(aNumbers[i]);
sMessage += aNumbers[i] + "<br>";
}
sMessage += "Total:" + iTotal;
document.write(sMessage);
</script>
Continue continue
The code copy is as follows:
<script language="javascript">
var aNumbers = new Array();
var sMessage = "You entered:<br>";
var iTotal = 0;
var vUserInput;
var iArrayIndex = 0;
do{
vUserInput = Number(prompt("Enter a number, or '0'Exit","0"));
if(isNaN(vUserInput)){
alert("Input error, please enter the number, '0' exit");
Continue; // If the input error occurs, the current loop will be exited and the next loop will be continued
}
aNumbers[iArrayIndex] = vUserInput;
iArrayIndex++;
}while(vUserInput != 0) // Exit the loop body when the input is 0 (default value).
// Common methods for looping through arrays:
for(var i=0;i<aNumbers.length;i++){
iTotal += Number(aNumbers[i]);
sMessage += aNumbers[i] + "<br>";
}
sMessage += "Total:" + iTotal;
document.write(sMessage);
</script>
Do you guys know the difference and connection between these two sentences?