If the statement is true, the code inside of {} would be executed.
When the first execution is done, the system will return to the while to repeat the execution.
The loop will stop until the result becomes false.
var n=0;
while(n<=50) {
n++;
}
alert(n);
var sum=0;
var i=1;
while(i<=100){
sum=sum+i; → it can be written as sum+=i as well.
i++;
}
alert(sum);
var sum=0;
for(var i=1;i<=100;i++){
sum=sum+i;
}
alert(sum);
Example 1. while statement x break
var n=0
while(n<=100){
if(n==50){
break;
}
n++;
}
alert(n);
=>n is 50.
Example 2. for statement x continue
var x=0;
for(var n=0;n<=100;n++){
if(n%4==0){ → n can be divided exactly by 4.
continue;
}
x++;
}
alert(x);
=>x is 75.
Feel free to comment and share your ideas below to learn together!
If you guys find this article helpful, please kindly do the writer a favor — LIKE this article.