{"id":60,"date":"2026-03-01T15:43:09","date_gmt":"2026-03-01T10:43:09","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=60"},"modified":"2026-03-13T09:47:27","modified_gmt":"2026-03-13T04:47:27","slug":"while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/","title":{"rendered":"While Loop"},"content":{"rendered":"\n<p>A <strong>while loop<\/strong> is used to repeat a block of code as long as a condition is <strong>True<\/strong>.<br>It is commonly used when the number of repetitions is not known in advance.<\/p>\n\n\n\n<p>The loop keeps running until the condition becomes False.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>SYNTAX OF WHILE LOOP<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">while condition:<br>    # code block<\/pre>\n\n\n\n<p>If the condition is True, the loop runs.<br>If the condition becomes False, the loop stops.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>EXAMPLE 1: BASIC WHILE LOOP<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">count = 1while count &lt;= 5:<br>    print(count)<br>    count += 1<\/pre>\n\n\n\n<p>This loop prints numbers from 1 to 5.<br>The <code>count += 1<\/code> increases the value each time, preventing an infinite loop.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>INFINITE LOOP<\/strong><\/h1>\n\n\n\n<p>If the condition never becomes False, the loop runs forever.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">while True:<br>    print(\"This will run forever\")<\/pre>\n\n\n\n<p>To stop an infinite loop manually, press Ctrl + C.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHILE LOOP WITH IF CONDITION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">num = 1while num &lt;= 10:<br>    if num % 2 == 0:<br>        print(num)<br>    num += 1<\/pre>\n\n\n\n<p>This prints even numbers from 1 to 10.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>BREAK STATEMENT IN WHILE LOOP<\/strong><\/h1>\n\n\n\n<p>The <code>break<\/code> statement stops the loop immediately.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num = 1while num &lt;= 5:<br>    if num == 4:<br>        break<br>    print(num)<br>    num += 1<\/pre>\n\n\n\n<p>The loop stops when <code>num<\/code> becomes 4.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>CONTINUE STATEMENT IN WHILE LOOP<\/strong><\/h1>\n\n\n\n<p>The <code>continue<\/code> statement skips the current iteration and moves to the next one.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num = 0while num &lt; 5:<br>    num += 1<br>    if num == 3:<br>        continue<br>    print(num)<\/pre>\n\n\n\n<p>This skips printing the number 3.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHILE LOOP WITH ELSE<\/strong><\/h1>\n\n\n\n<p>A while loop can also have an else block.<br>The else block runs when the loop finishes normally (not stopped by break).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">num = 1while num &lt;= 3:<br>    print(num)<br>    num += 1<br>else:<br>    print(\"Loop finished successfully\")<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>IMPORTANT POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Always make sure the condition eventually becomes False<br>\u2022 Update the loop variable inside the loop<br>\u2022 Use break and continue for better control<br>\u2022 Be careful to avoid infinite loops<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY WHILE LOOPS ARE IMPORTANT<\/strong><\/h1>\n\n\n\n<p>\u2022 Useful when repetitions depend on a condition<br>\u2022 Helpful for user input validation<br>\u2022 Used in real-world applications like login systems and menu-driven programs<\/p>\n\n\n\n<p>Understanding the <strong>while loop<\/strong> helps you create dynamic and condition-based Python programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FUNDAMENTALS (PYF) > Control Flow > While Loop<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773377419839\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":19,"template":"","class_list":["post-60","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>While Loop - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.\" \/>\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\/python\/lesson\/while-loop\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"While Loop - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/while-loop\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T04:47:27+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/while-loop\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/while-loop\\\/\",\"name\":\"While Loop - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T10:43:09+00:00\",\"dateModified\":\"2026-03-13T04:47:27+00:00\",\"description\":\"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/while-loop\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/while-loop\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/while-loop\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FUNDAMENTALS (PYF) > Control Flow > While Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?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":"While Loop - One Language. Endless Possibilities","description":"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.","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\/python\/lesson\/while-loop\/","og_locale":"en_US","og_type":"article","og_title":"While Loop - One Language. Endless Possibilities","og_description":"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.","og_url":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T04:47:27+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/","url":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/","name":"While Loop - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T10:43:09+00:00","dateModified":"2026-03-13T04:47:27+00:00","description":"Learn Python while loops to repeat code based on conditions, handle input, and control flow using break, continue, and else.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/while-loop\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/while-loop\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FUNDAMENTALS (PYF) > Control Flow > While Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}