{"id":91,"date":"2026-05-01T15:25:07","date_gmt":"2026-05-01T15:25:07","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=91"},"modified":"2026-05-01T15:25:08","modified_gmt":"2026-05-01T15:25:08","slug":"do-while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop","title":{"rendered":"Do while Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<br>The do while loop in JavaScript is used to execute a block of code at least once, and then repeat the execution as long as a specified condition is true. This loop is useful when you want the code to run before checking the condition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What is a Do While Loop<br>A do while loop first runs the code, then checks the condition. If the condition is true, the loop continues. If false, it stops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax<br>do {<br>code to execute<br>} while (condition);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How It Works<br>The code inside the loop runs one time no matter what<br>After execution, the condition is checked<br>If the condition is true, the loop runs again<br>If the condition is false, the loop stops<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<br>let i = 1;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">do {<br>console.log(i);<br>i++;<br>} while (i &lt;= 5);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, the loop prints numbers from 1 to 5.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Key Difference from While Loop<br>The do while loop always runs at least once<br>The while loop may not run if the condition is false at the start<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When to Use Do While Loop<br>When you need the code to execute at least once<br>When user input is required before validation<br>When working with menus or repeated prompts<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Advantages<br>Ensures at least one execution<br>Simple and easy to use<br>Useful for user-driven programs<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Mistake<br>Forgetting to update the condition variable can cause an infinite loop<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<br>The do while loop is an important control structure in JavaScript. It is best used when you want the code to run first and check the condition later.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">JavaScript Fundamentals (Beginner Level) > Loops > do 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-1777649083599\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777649083402\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-91","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Do while Loop - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.\" \/>\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\/javascript\/?lesson=do-while-loop\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Do while Loop - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-01T15:25:08+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\\\/javascript\\\/?lesson=do-while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=do-while-loop\",\"name\":\"Do while Loop - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-01T15:25:07+00:00\",\"dateModified\":\"2026-05-01T15:25:08+00:00\",\"description\":\"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=do-while-loop#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=do-while-loop\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=do-while-loop#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Fundamentals (Beginner Level) > Loops > do while Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?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":"Do while Loop - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.","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\/javascript\/?lesson=do-while-loop","og_locale":"en_US","og_type":"article","og_title":"Do while Loop - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-01T15:25:08+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\/javascript\/?lesson=do-while-loop","url":"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop","name":"Do while Loop - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-01T15:25:07+00:00","dateModified":"2026-05-01T15:25:08+00:00","description":"Learn JavaScript do while loop with examples. Understand syntax, usage, and differences from while loop easily.","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=do-while-loop#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"JavaScript Fundamentals (Beginner Level) > Loops > do while Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}