1

Below is snippet of code for breaking the Execution of statement using break statement

 for(st=1;st<=20;st=st+5) {
    if(st == 15) {
       break;
    }
    document.write(st+"<br>");
 }

output comes

1-6-11-16

I don't understand why 16 appear as loop should break on 15..

0

4 Answers 4

8

Your st NEVER hits 15, so the if() never triggers. You probably want if (st >= 15) instead, so you can check for "15 or larger".

iteration #1: st = 1       st == 15 -> false
iteration #2: st = 6       st == 15 -> false
iteration #3: st = 11      st == 15 -> false
iteration #4: st = 16      st == 15 -> false

v.s.

iteration #1: st = 1       st >= 15 -> false
iteration #2: st = 6       st >= 15 -> false
iteration #3: st = 11      st >= 15 -> false
iteration #4: st = 16      st >= 15 -> true
Sign up to request clarification or add additional context in comments.

1 Comment

plus1 for the iteration table
3

Your value starts at 1, not 0. So you never hit 15 exactly.

Comments

1

you should use

for(st=1;st<=20;st=st+5){
if(st >= 15)
break;
else console.log(st);}

Comments

0

Your statement starts with 1 so it will never hit 15.If you want to hit 15 here you are

for(st=0;st<=20;st=st+5) {
    if(st == 15) {
       break;
    }
    document.write(st+"<br>");
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.