{"id":16114,"date":"2020-07-11T09:25:26","date_gmt":"2020-07-11T09:25:26","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=16114"},"modified":"2022-04-28T09:15:22","modified_gmt":"2022-04-28T09:15:22","slug":"php-7-fundamental-tutorial-for-beginners-php-break-and-continue-statement","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-7-fundamental-tutorial-for-beginners-php-break-and-continue-statement\/","title":{"rendered":"PHP 7 Fundamental Tutorial for Beginners \u2013 PHP Break and Continue Statement"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Break statement<\/h2>\n\n\n\n<p>The break statement is used with the conditional switch statement and with the do, for, and while loop statements. When a break statement is encountered inside a loop, the loop is immediately&nbsp;<strong>terminated<\/strong>&nbsp;and the program control resumes at the next statement following the loop.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Break.png\" alt=\"\" class=\"wp-image-16119\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>A break statement can be used to terminate or to come out from the loop or conditional statement unconditionally.<\/li><li>It can be used in switch statement to break and come out from the switch statement after each case expression.<\/li><li>Whenever, break statement is encounter within the program then it will break the current loop or block.<\/li><li>A break statement is normally used with if statement.<\/li><li>When certain condition becomes true to terminate the loop then break statement can be used<\/li><\/ul>\n\n\n\n<p>This example jumps out of the loop when x is equal to 4:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/7eedb822524a4a9db93f40d8929de313.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Output:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0\nThe number is: 1\nThe number is: 2\nThe number is: 3<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Continue statement<\/h2>\n\n\n\n<p>Continue statement works like break but instead of forcing termination, it forces the&nbsp;<strong>next iteration<\/strong>&nbsp;of the loop to take place and skipping the rest of the code. Continue statement is mostly used inside loops, whenever it is encountered inside a loop, either conditionally or unconditionally, transfers control to the next iteration of either the current loop or an enclosing labelled loop.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/Continue.png\" alt=\"\" class=\"wp-image-16130\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li>A&nbsp;<em>continue<\/em>&nbsp;statement can be used into the loop when we want to skip some statement to be executed and continue the execution of above statement based on some specific condition.<\/li><li>Similar to break statement,&nbsp;<em>continue<\/em>&nbsp;is also used with if statement.<\/li><li>When compiler encounters continue, statements after continue are skipped and control transfers to the statement above continue.<\/li><\/ul>\n\n\n\n<p>This example skips the value of 4:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/a5fab719e39d591ae04033be8d751193.js\"><\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Output:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">The number is: 0\nThe number is: 1\nThe number is: 2\nThe number is: 3\nThe number is: 5\nThe number is: 6\nThe number is: 7\nThe number is: 8\nThe number is: 9\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Break and Continue in While Loop<\/strong><\/h2>\n\n\n\n<p>You can also use&nbsp;<code>break<\/code>&nbsp;and&nbsp;<code>continue<\/code>&nbsp;in&nbsp;<code>while<\/code>&nbsp;loops:<\/p>\n\n\n\n<p><strong>Break Example<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/48890eb3f6c04ddb63e2ddbdec529b8d.js\"><\/script>\n\n\n\n<p><strong>Continue Example<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/1411940bd5aed9150543954d0868c270.js\"><\/script>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_89833\"><iframe loading=\"lazy\"  id=\"_ytid_57262\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_89833\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Break statement The break statement is used with the conditional switch statement and with the do, for, and while loop statements. When a break statement is encountered inside a loop,&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[6248,6249],"class_list":["post-16114","post","type-post","status-publish","format-standard","hentry","category-php","tag-break-statement","tag-continue-statement"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=16114"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16114\/revisions"}],"predecessor-version":[{"id":24673,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16114\/revisions\/24673"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=16114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=16114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=16114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}