{"id":41,"date":"2026-03-03T09:00:32","date_gmt":"2026-03-03T09:00:32","guid":{"rendered":"https:\/\/gigz.pk\/r\/?post_type=lesson&#038;p=41"},"modified":"2026-04-01T11:21:09","modified_gmt":"2026-04-01T11:21:09","slug":"loops-and-conditional-statements","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/r\/lesson\/loops-and-conditional-statements\/","title":{"rendered":"Loops and Conditional Statements"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Loops and conditional statements are fundamental programming constructs in R that allow you to control the flow of your code, repeat tasks, and make decisions based on conditions.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>1. Conditional Statements<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Conditional statements allow your code to take different actions depending on whether a condition is TRUE or FALSE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a) if Statement<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">x &lt;- 10if (x &gt; 5) {<br>  print(\"x is greater than 5\")<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b) if-else Statement<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">x &lt;- 3if (x &gt; 5) {<br>  print(\"x is greater than 5\")<br>} else {<br>  print(\"x is 5 or less\")<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c) if-else if-else Statement<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">x &lt;- 7if (x &gt; 10) {<br>  print(\"x is greater than 10\")<br>} else if (x &gt; 5) {<br>  print(\"x is between 6 and 10\")<br>} else {<br>  print(\"x is 5 or less\")<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>2. Loops in R<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Loops allow you to repeat a block of code multiple times.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>a) for Loop<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Used to iterate over a sequence or vector:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">for (i in 1:5) {<br>  print(paste(\"Iteration\", i))<br>}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Looping through a vector:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">names &lt;- c(\"Alice\", \"Bob\", \"Charlie\")for (name in names) {<br>  print(paste(\"Hello\", name))<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>b) while Loop<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repeats a block of code as long as a condition is TRUE:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">count &lt;- 1while (count &lt;= 5) {<br>  print(paste(\"Count is\", count))<br>  count &lt;- count + 1<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>c) repeat Loop<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repeats code indefinitely until a <code>break<\/code> condition is met:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">count &lt;- 1repeat {<br>  print(paste(\"Count is\", count))<br>  count &lt;- count + 1<br>  if (count &gt; 5) {<br>    break<br>  }<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>3. Combining Loops and Conditional Statements<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">You can use conditional statements inside loops for more control:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">numbers &lt;- c(1, 3, 5, 6, 8)for (num in numbers) {<br>  if (num %% 2 == 0) {<br>    print(paste(num, \"is even\"))<br>  } else {<br>    print(paste(num, \"is odd\"))<br>  }<br>}<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>4. Advantages of Using Loops and Conditionals<\/strong><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automate repetitive tasks efficiently<\/li>\n\n\n\n<li>Make programs dynamic and flexible<\/li>\n\n\n\n<li>Allow decision-making based on conditions<\/li>\n\n\n\n<li>Essential for data processing, simulations, and iterative calculations<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Loops and conditional statements are essential tools in R programming. They allow you to automate repetitive tasks, perform calculations dynamically, and control the flow of your code based on conditions. Mastering these constructs is a key step toward becoming a proficient R programmer.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1775042421804\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/r\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">R Programming (R Lang) > Programming in R > Loops and Conditional Statements<\/span><\/span><\/div>","protected":false},"menu_order":18,"template":"","class_list":["post-41","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.<\/title>\n<meta name=\"description\" content=\"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"og:description\" content=\"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Analyze Deep. Visualize Better. Build with R.\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-01T11:21:09+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/lesson\\\/loops-and-conditional-statements\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\"},\"datePublished\":\"2026-03-03T09:00:32+00:00\",\"dateModified\":\"2026-04-01T11:21:09+00:00\",\"description\":\"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R Programming (R Lang) > Programming in R > Loops and Conditional Statements\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/r\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/r\\\/\",\"name\":\"Analyze Deep. Visualize Better. Build with R.\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/r\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.","description":"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.","og_description":"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Analyze Deep. Visualize Better. Build with R.","article_modified_time":"2026-04-01T11:21:09+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/r\/lesson\/loops-and-conditional-statements\/","url":"https:\/\/gigz.pk\/","name":"Loops and Conditional Statements - Analyze Deep. Visualize Better. Build with R.","isPartOf":{"@id":"https:\/\/gigz.pk\/r\/#website"},"datePublished":"2026-03-03T09:00:32+00:00","dateModified":"2026-04-01T11:21:09+00:00","description":"Learn how to use loops and conditional statements in R with examples. Master if-else, for loops, while loops, and control flow.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/r\/"},{"@type":"ListItem","position":2,"name":"R Programming (R Lang) > Programming in R > Loops and Conditional Statements"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/r\/#website","url":"https:\/\/gigz.pk\/r\/","name":"Analyze Deep. Visualize Better. Build with R.","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/r\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/r\/wp-json\/wp\/v2\/media?parent=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}