

{"id":116397,"date":"2023-08-31T19:06:38","date_gmt":"2023-08-31T13:36:38","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116397"},"modified":"2023-08-31T19:07:03","modified_gmt":"2023-08-31T13:37:03","slug":"kotlin-break-return-continue-statement","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/","title":{"rendered":"Kotlin Break, Return and Continue Statement"},"content":{"rendered":"<p>In Kotlin, return and jump statements play a crucial role in controlling the flow of execution within a program. These statements allow developers to exit or skip certain sections of code based on specific conditions. In this article, we will explore the concepts of break, continue, and return statements in Kotlin.<\/p>\n<h3>Break Statement in Kotlin<\/h3>\n<p>The break statement is used to terminate the execution of a loop, such as a for loop or a while loop, prematurely. It allows you to exit the loop even if the loop condition is not met. Let&#8217;s consider an example to illustrate its usage:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun main() {\r\n    for (i in 1..10) {\r\n        if (i == 5) {\r\n            break\r\n        }\r\n        println(i)\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n3<br \/>\n4<\/div>\n<p><strong>Code Explanation<\/strong><\/p>\n<p>In the above code, the loop will terminate when `i` becomes 5 due to the `break` statement. As a result, only the numbers 1 to 4 will be printed.<\/p>\n<h3>Kotlin Continue Statement:<\/h3>\n<p>The continue statement is used to skip the rest of the code block within a loop iteration and move to the next iteration. It is commonly used to skip certain iterations based on specific conditions. Let&#8217;s see an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun main() {\r\n    for (i in 1..5) {\r\n        if (i == 3) {\r\n            continue\r\n        }\r\n        println(i)\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">1<br \/>\n2<br \/>\n4<br \/>\n5<\/div>\n<p><strong>Code Explanation<\/strong><\/p>\n<p>In the above code, when `i` is equal to 3, the continue statement is encountered, and the loop skips printing that number. The loop continues with the next iteration, printing all other numbers.<\/p>\n<h3>Return Statement in Kotlin<\/h3>\n<p>The return statement is used to exit a function or a lambda expression and return a value. It can also be used to terminate the execution of a block of code in a larger scope. Here&#8217;s an example that demonstrates the usage of return:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">fun sum(a: Int, b: Int): Int {\r\n    val total = a + b\r\n    return total\r\n}\r\n\r\nfun main() {\r\n    val result = sum(3, 5)\r\n    println(\"Sum: $result\")\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Sum: 8<\/div>\n<p><strong>Code Explanation<\/strong><\/p>\n<p>In the above code, the sum() function takes two integers as parameters, calculates their sum, and returns the result using the return statement. The main() function then calls the sum() function and prints the returned value.<\/p>\n<h3>Conclusion:<\/h3>\n<p>In Kotlin, return and jump statements provide developers with powerful tools to control the flow of execution in their programs. The break statement allows premature termination of loops, the continue statement skips iterations, and the return statement exits functions or lambda expressions. Understanding and using these statements effectively can greatly enhance the flexibility and control of your Kotlin programs.<\/p>\n<p>Happy Coding! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Kotlin, return and jump statements play a crucial role in controlling the flow of execution within a program. These statements allow developers to exit or skip certain sections of code based on specific&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":116399,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27775],"tags":[27975,27977,27974,27976],"class_list":["post-116397","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kotlin-tutorials","tag-kotlin-break-statement","tag-kotlin-continue-statement","tag-kotlin-return-and-jump","tag-kotlin-return-statement"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Kotlin Break, Return and Continue Statement - DataFlair<\/title>\n<meta name=\"description\" content=\"In Kotlin, return and jump statements provide developers with tools to control the flow of execution in their programs. Learn with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Kotlin Break, Return and Continue Statement - DataFlair\" \/>\n<meta property=\"og:description\" content=\"In Kotlin, return and jump statements provide developers with tools to control the flow of execution in their programs. Learn with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-31T13:36:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-31T13:37:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Kotlin Break, Return and Continue Statement - DataFlair","description":"In Kotlin, return and jump statements provide developers with tools to control the flow of execution in their programs. Learn with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/","og_locale":"en_US","og_type":"article","og_title":"Kotlin Break, Return and Continue Statement - DataFlair","og_description":"In Kotlin, return and jump statements provide developers with tools to control the flow of execution in their programs. Learn with examples.","og_url":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-31T13:36:38+00:00","article_modified_time":"2023-08-31T13:37:03+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Kotlin Break, Return and Continue Statement","datePublished":"2023-08-31T13:36:38+00:00","dateModified":"2023-08-31T13:37:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/"},"wordCount":365,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp","keywords":["kotlin break statement","kotlin continue statement","Kotlin Return and Jump","kotlin return statement"],"articleSection":["Kotlin Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/","url":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/","name":"Kotlin Break, Return and Continue Statement - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp","datePublished":"2023-08-31T13:36:38+00:00","dateModified":"2023-08-31T13:37:03+00:00","description":"In Kotlin, return and jump statements provide developers with tools to control the flow of execution in their programs. Learn with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/07\/return-and-jump-statement-in-kotlin.webp","width":1200,"height":628,"caption":"return and jump statement in kotlin"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/kotlin-break-return-continue-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Kotlin Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/kotlin-tutorials\/"},{"@type":"ListItem","position":3,"name":"Kotlin Break, Return and Continue Statement"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=116397"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116397\/revisions"}],"predecessor-version":[{"id":120059,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116397\/revisions\/120059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/116399"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}