

{"id":4363,"date":"2017-09-28T08:37:58","date_gmt":"2017-09-28T03:07:58","guid":{"rendered":"http:\/\/data-flair.training\/blogs\/?p=4363"},"modified":"2020-02-03T16:56:58","modified_gmt":"2020-02-03T11:26:58","slug":"r-control-structures","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/r-control-structures\/","title":{"rendered":"Control Structures in R &#8211; Master the Working of Loops in R!"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1467,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/R_(programming_language)&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251001042859\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/R_(programming_language)&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 08:17:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-12 12:22:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-15 12:29:21&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 15:20:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-21 18:00:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-25 04:08:57&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-28 06:54:42&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-31 09:47:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-03 17:14:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-06 19:17:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-09 21:09:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-13 04:31:41&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-16 15:06:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-19 19:03:58&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-23 05:30:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-26 10:18:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-29 11:45:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-01 12:00:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-04 12:09:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-07 15:09:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-10 18:01:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-13 23:45:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-17 05:29:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 07:23:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-23 10:05:24&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-26 14:54:33&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-03-01 16:00:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-04 19:56:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-08 03:19:57&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-11 07:47:37&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-14 13:54:22&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-17 17:24:09&quot;,&quot;http_code&quot;:429},{&quot;date&quot;:&quot;2026-03-20 23:04:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-24 00:07:34&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-27 00:15:51&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-30 08:20:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-03 14:48:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-06 19:55:35&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-10 05:52:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 07:47:26&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-16 08:05:10&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-19 13:04:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-22 13:52:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-25 13:58:03&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-29 01:16:25&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-02 04:13:02&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-05 06:33:38&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-08 17:48:43&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-12 03:38:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-15 04:53:06&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-18 09:15:30&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-21 12:35:48&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-25 03:51:04&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 07:13:15&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-01 04:45:49&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-04 06:40:14&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-04 06:40:14&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>In order to control the execution of the expressions\u00a0flow in R, we make use of the control structures. These control structures are also called as loops in R. There are eight types of control structures in R:<\/p>\n<ul>\n<li>if<\/li>\n<li>if-else<\/li>\n<li>for<\/li>\n<li>nested loops<\/li>\n<li>while<\/li>\n<li>repeat and break<\/li>\n<li>next<\/li>\n<li>return<\/li>\n<\/ul>\n<p>We will study their examples as well as its execution flow. So, let&#8217;s start with the tutorial!<\/p>\n<h2>Control Structures in R Programming<\/h2>\n<p>R provides various standard control structures for our requirements. The expr expression consists of multiple statements that can be enclosed in braces {}.<\/p>\n<div class=\"ci\">It is more efficient to use built-in functions in R rather than control structures, whenever possible. This facilitates the flow of execution to be controlled inside a function.<\/div>\n<p><em>Control structures define the flow of the program.\u00a0The decision is then made after the variable is assessed.\u00a0<\/em><\/p>\n<p><em><strong>Wait! Have you checked &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/r-factor-functions\/\">R Factor Functions Tutorial<\/a><\/strong><\/em><\/p>\n<h2>List of R Control Structures with Examples<\/h2>\n<p>Now we will discuss the control structures in R one by one in detail:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-55843\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg\" alt=\"Control Structures in R Programming\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h3>1. if Condition in R<\/h3>\n<p>This task is carried out only if this condition is returned as TRUE. R makes it even easier: You can drop the word <em>then<\/em> and specify your choice in an if statement.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (test_expression) {\r\nstatement\r\n}<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">values &lt;- 1:10                                        \r\nif (sample(values,1) &lt;= 10)                               \r\nprint(paste(values, \"is less than or equal to 10\"))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63091\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110.jpg\" alt=\"values 1:10\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>You must check <a href=\"https:\/\/data-flair.training\/blogs\/r-data-types\/\">R Data Types<\/a>\u00a0as it plays an important role in Control Structures<\/strong><\/em><\/p>\n<h3>2. if-else Condition in R<\/h3>\n<p>An <em>if\u2026else<\/em> statement contains the same elements as an if statement (see the preceding section), with some extra elements:<\/p>\n<ul>\n<li>The keyword else, placed after the first code block.<\/li>\n<li>The second block of code, contained within braces, that has to be carried out, only if the result of the condition in the if() statement is FALSE.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">if (test_expression) {\r\nstatement\r\n} else {\r\nstatement\r\n}<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">val1 = 10                                    #Creating our first variable val1\r\nval2 = 5                                     #Creating second variable val2\r\nif (val1 &gt; val2){                            #Executing Conditional Statement based on the comparison \r\n  print(\"Value 1 is greater than Value 2\")\r\n} else if (val1 &lt; val2){\r\n  print(\"Value 1 is less than Value 2\")\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63093\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2.jpg\" alt=\"val1 and val2\" width=\"1299\" height=\"802\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2-150x93.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2-300x185.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2-768x474.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2-1024x632.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val1-and-val2-520x321.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>3. for Loop in R<\/h3>\n<p>A loop is a sequence of instructions that is repeated until a certain condition is reached. for, while and repeat, with the additional clauses break and next are used to construct loops.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>These control structures in <a href=\"https:\/\/en.wikipedia.org\/wiki\/R_(programming_language)\">R<\/a>, made of the rectangular box \u2018init&#8217; and the diamond. It is executed a known number of times. <em>for<\/em>\u00a0is a block that is contained within curly braces.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">values &lt;- c(1,2,3,4,5) \r\nfor(id in 1:5){ \r\n  print(values[id])\r\n  }<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63095\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1.jpg\" alt=\"values c 12345\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-c-12345-1-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>4. Nested Loop in R<\/h3>\n<p>It is similar to the standard for <strong>loop<\/strong>, which makes it easy to convert <strong>for<\/strong> <strong>loop<\/strong> to a <strong>foreach loop<\/strong>. Unlike many parallel programming packages for R, foreach doesn&#8217;t require the body of for loop to be turned into a function. We can call this a nesting operator because it is used to create nested foreach loops.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">mat &lt;- matrix(1:10, 2)\r\nfor (id1 in seq(nrow(mat))) {\r\n  for (id2 in seq(ncol(mat))) {\r\n    print(mat[id1, id2])\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63092\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10.jpg\" alt=\"Matrix 1:2, 10\" width=\"1299\" height=\"1008\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10-150x116.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10-300x233.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10-768x596.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10-1024x795.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Matrix-12-10-520x404.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Do you know &#8211;\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/rstudio-tutorial\/\">How to Import and Transform Data with RStudio<\/a><\/strong><\/em><\/p>\n<h3>5. while Loop in R<\/h3>\n<p>The format is<strong> while(cond) expr<\/strong>, where <strong>cond<\/strong> is the condition to test and <strong>expr<\/strong> is an expression.<\/p>\n<p>R would complain about the missing expression that was supposed to provide the required True or False and in fact, it does not know \u2018response\u2019 before using it in the loop. We can also do this because, if we answer right at first attempt, the loop will not be executed at all.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">val = 2.987\r\nwhile(val &lt;= 4.987) {\r\n  val = val + 0.987\r\n  print(c(val,val-2,val-1))\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63097\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987.jpg\" alt=\"val=2.987\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val2.987-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p>Be sure there is a way to exit out of a while loop.<\/p>\n<h3>6. repeat and break Statement in R<\/h3>\n<p>We use <strong>break<\/strong> statement inside a loop (repeat, for, while) to stop the iterations and flow the control outside of the loop. While in a nested looping situation, where there is a loop inside another loop, this statement exits from the innermost loop that is being evaluated.<\/p>\n<p>A <strong>repeat<\/strong> loop is used to iterate over a block of code, multiple numbers of times. There is no condition check in a repeat loop to exit the loop. We ourselves put a condition explicitly inside the body of the loop and use the break statement to exit the loop. Failing to do so will result in an infinite loop.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">repeat {\r\n# simulations; generate some value have an expectation if within some range,\r\n# then exit the loop\r\nif ((value - expectation) &lt;= threshold) {\r\nbreak\r\n}\r\n}<\/pre>\n<p>The repeat loop is an infinite loop and is used in association with a break statement.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>Below, the code shows a repeat statement in R:<\/p>\n<p>A break statement is used in a loop to stop the iterations and flow the control outside of the loop.<\/p>\n<p><strong>Example of Repeat Statement in R:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">val &lt;- 5\r\nrepeat {\r\n  print(val)\r\n  val &lt;- val+1\r\n  if (val == 10){\r\n    break\r\n  }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63098\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5.jpg\" alt=\"val&lt;-5 (Control Structures in R)\" width=\"1299\" height=\"943\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5-150x109.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5-300x218.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5-768x558.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5-1024x743.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/val-5-520x377.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><strong>Example of Break Statement in R:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">values = 1:10\r\nfor (id in values){\r\n  if (id == 2){\r\n    break\r\n  }\r\n  print(id)\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63099\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values.jpg\" alt=\"values 1:10 for id in values\" width=\"1299\" height=\"741\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values-150x86.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values-300x171.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values-768x438.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values-1024x584.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/values-110-for-id-in-values-520x297.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<p><em><strong>Recommended Reading<\/strong>\u00a0&#8211;\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/r-vector-functions\/\"><strong>R Vector Functions<\/strong><\/a><\/em><\/p>\n<h3>7. next Statement in R<\/h3>\n<p><strong>next<\/strong> jumps to the next cycle without completing a particular iteration. In fact, it jumps to the evaluation of the condition holding the current loop. Next statement enables to skip the current iteration of a loop without terminating it.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">x = 1: 4\r\nfor (i in x) {\r\nif (i == 2) {\r\nnext\r\n}\r\nprint(i)\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63103\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14.jpg\" alt=\"x = 1:4 (Control Structures in R)\" width=\"1299\" height=\"846\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14-150x98.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14-300x195.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14-768x500.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14-1024x667.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/x-14-520x339.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h3>8. return Statement in R<\/h3>\n<p>Many times, we will require\u00a0some functions to do processing and return back the result. This is accomplished with the <strong>return()<\/strong>\u00a0statement in R.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">return(expression)<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">check &lt;- function(x) {\r\nif (x &gt; 0) {\r\nresult &lt;- \"Positive\"\r\n} else if (x &lt; 0) {\r\nresult &lt;- \"Negative\"\r\n} else {\r\nresult &lt;- \"Zero\"\r\n}\r\nreturn(result)\r\n}\r\n<\/pre>\n<p>In the console window, we type:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; check(1)\r\n&gt; check(-10)\r\n&gt; check(0)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-63102\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx.jpg\" alt=\"check &lt;- function(x) - Control Structures in R\" width=\"1299\" height=\"810\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx.jpg 1299w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx-150x94.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx-300x187.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx-768x479.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx-1024x639.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/check-functionx-520x324.jpg 520w\" sizes=\"auto, (max-width: 1299px) 100vw, 1299px\" \/><\/a><\/p>\n<h2>Wrapping up the Use of Loops in R<\/h2>\n<ul>\n<li>Be careful when you use the repeat statement.<\/li>\n<li>Make sure that termination is explicitly set by testing a condition or we can end up in an infinite loop.<\/li>\n<li>It is better to use one or more function calls within the loop if a loop is getting (too) big.<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>Control Structures in R are the backbone of any programming language, as it changes the flow of the program. In this R tutorial, we have studied in detail about the control statements along with its syntax and uses and how to operate these loops with different control structures.<\/p>\n<p><em><strong>Now, it&#8217;s the time for exploring the <a href=\"https:\/\/data-flair.training\/blogs\/r-function\/\">Usage of R Function<\/a><\/strong><\/em><\/p>\n<p>Hope you liked our explanation. Any queries? Feel free to share in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In order to control the execution of the expressions\u00a0flow in R, we make use of the control structures. These control structures are also called as loops in R. There are eight types of control&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":55843,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[20416,20417],"class_list":["post-4363","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-control-structures-in-r","tag-loops-uses-in-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Control Structures in R - Master the Working of Loops in R! - DataFlair<\/title>\n<meta name=\"description\" content=\"Understand the different types of R control structures with their usage, syntax, statement and example in detail. You can&#039;t afford to miss this one!\" \/>\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\/r-control-structures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Control Structures in R - Master the Working of Loops in R! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Understand the different types of R control structures with their usage, syntax, statement and example in detail. You can&#039;t afford to miss this one!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/r-control-structures\/\" \/>\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=\"2017-09-28T03:07:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-02-03T11:26:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Control Structures in R - Master the Working of Loops in R! - DataFlair","description":"Understand the different types of R control structures with their usage, syntax, statement and example in detail. You can't afford to miss this one!","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\/r-control-structures\/","og_locale":"en_US","og_type":"article","og_title":"Control Structures in R - Master the Working of Loops in R! - DataFlair","og_description":"Understand the different types of R control structures with their usage, syntax, statement and example in detail. You can't afford to miss this one!","og_url":"https:\/\/data-flair.training\/blogs\/r-control-structures\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-09-28T03:07:58+00:00","article_modified_time":"2020-02-03T11:26:58+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Control Structures in R &#8211; Master the Working of Loops in R!","datePublished":"2017-09-28T03:07:58+00:00","dateModified":"2020-02-03T11:26:58+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/"},"wordCount":928,"commentCount":1,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg","keywords":["Control Structures in R","Loops Uses in R"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/r-control-structures\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/","url":"https:\/\/data-flair.training\/blogs\/r-control-structures\/","name":"Control Structures in R - Master the Working of Loops in R! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg","datePublished":"2017-09-28T03:07:58+00:00","dateModified":"2020-02-03T11:26:58+00:00","description":"Understand the different types of R control structures with their usage, syntax, statement and example in detail. You can't afford to miss this one!","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/r-control-structures\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/09\/Control-Structures-in-R-Programming.jpg","width":802,"height":420,"caption":"Control Structures in R Programming"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/r-control-structures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"R Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/r\/"},{"@type":"ListItem","position":3,"name":"Control Structures in R &#8211; Master the Working of Loops in R!"}]},{"@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\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4363","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=4363"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4363\/revisions"}],"predecessor-version":[{"id":63104,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/4363\/revisions\/63104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/55843"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=4363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=4363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=4363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}