Tìm hiểu về lệnh break và continue trong Javascript

1: Câu lệnh break

• Lệnh break dùng để thoát khỏi vòng lặp mà không đi tới điểm cuối

• Khi gặp câu lệnh này trong vòng lặp, chương trình sẽ thoát ra khỏi vòng lặp và chỉ đến câu lệnh liền sau nó

• Nếu nhiều vòng lặp thì break sẽ thoát ra khỏi vòng lặp gần nhất.

• Ngoài ra, break còn được dùng trong cấu trúc lựa chọn switch

Ví dụ:

for(Student s : listStudent){

    code block 1

     break;

  code block 2

}

Khối lệnh 1 vẫn được thực hiện nhưng khối lệnh 2 thì không vì ta đã đặt break để dừng ngay sau khi thực thi khối lệnh 1

2 : Câu lệnh continue

• Lệnh continue để nhảy qua một phần của khối lệnh và bắt đầu dòng tiếp theo.

• Khi gặp lệnh này trong các vòng lặp, chương trình sẽ bỏ qua phần còn lại trong vòng lặp và tiếp tục thực hiện lần lặp tiếp theo.

Ví dụ:

for (var i = 1; i <= 10; i++){ 

 if (i == 4) {   

   continue;  

}  

console.log(i);

}

//output: 1 2 3 5 6 7 8 9 10

Vòng lặp đã bỏ qua giá trị i =4

12 comments On Tìm hiểu về lệnh break và continue trong Javascript

  • I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back later. All the best

  • You actually make it appear really easy together with your presentation however I find this topic to be actually one thing which I believe I would never understand. It sort of feels too complex and very wide for me. I’m looking ahead on your next post, I’ll attempt to get the hold of it!

  • I enjoy you because of each of your labor on this web site. My niece really likes engaging in research and it is easy to understand why. Most of us know all of the dynamic form you give powerful tips and hints through your website and even inspire response from others about this article while our own girl is certainly being taught a whole lot. Take pleasure in the rest of the year. You have been performing a tremendous job.

  • Keep working ,terrific job!

  • hi!,I like your writing so much! share we communicate more about your post on AOL? I require an expert on this area to solve my problem. Maybe that’s you! Looking forward to see you.

  • I enjoy you because of each of your work on this website. My daughter take interest in managing research and it’s really simple to grasp why. All of us learn all about the compelling manner you make important techniques on this website and increase participation from some other people on this subject matter then our favorite simple princess is actually studying a great deal. Take advantage of the rest of the year. You’re the one performing a wonderful job.

  • fantástico este conteúdo. Gostei bastante. Aproveitem e vejam este site. informações, novidades e muito mais. Não deixem de acessar para se informar mais. Obrigado a todos e até mais. 🙂

  • Hello, you used to write great, but the last few posts have been kinda boring… I miss your great writings. Past few posts are just a bit out of track! come on!

  • hi!,I really like your writing so a lot! proportion we keep up a correspondence extra about your post on AOL? I need a specialist in this house to unravel my problem. Maybe that’s you! Looking ahead to see you.

  • Great post, I think website owners should acquire a lot from this web blog its really user friendly.

  • Hey just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I thought I’d post to let you know. The style and design look great though! Hope you get the problem solved soon. Kudos

  • I keep listening to the news bulletin lecture about getting free online grant applications so I have been looking around for the best site to get one. Could you tell me please, where could i get some?

Leave a Reply to best cloud mining Cancel Reply

Your email address will not be published.

Site Footer

Sliding Sidebar

Facebook