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
6 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.
Sliding Sidebar
Facebook