{"id":33,"date":"2026-05-20T14:45:49","date_gmt":"2026-05-20T14:45:49","guid":{"rendered":"https:\/\/gigz.pk\/csharp\/?post_type=lesson&#038;p=33"},"modified":"2026-05-20T17:33:19","modified_gmt":"2026-05-20T17:33:19","slug":"while-loop","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/csharp\/?lesson=while-loop","title":{"rendered":"while Loop"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The while loop in C# is used to repeat a block of code as long as a specified condition remains true. It is commonly used when the number of iterations is not known in advance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is while Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A while loop is a control structure that executes a block of code repeatedly until the given condition becomes false. It checks the condition before each iteration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Structure of while Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A while loop consists of a condition that is evaluated before the loop body. If the condition is true, the loop executes; otherwise, it stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How while Loop Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The program first checks the condition. If it is true, the code inside the loop runs. After execution, the condition is checked again. This process continues until the condition becomes false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Condition in while Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The condition determines whether the loop should continue or stop. It must eventually become false to prevent an infinite loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importance of while Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The while loop is useful when the number of repetitions is unknown. It helps in handling user input, reading data, and continuous processing tasks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Real World Usage<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">while loops are used in login attempts, game loops, data validation, and reading files until the end of content.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Infinite Loop Concept<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the condition never becomes false, the loop will run forever. This is called an infinite loop and should be avoided.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Mistakes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Forgetting to update loop variables<br>Incorrect condition logic<br>Creating infinite loops<br>Syntax errors<br>Not initializing variables properly<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practices<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Always update loop variables inside the loop<br>Ensure condition will eventually become false<br>Keep loop logic simple<br>Test with small inputs<br>Use meaningful variable names<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lesson Summary<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The while loop in C# is used for repeated execution based on a condition. It is essential for situations where the number of iterations is unknown and requires careful handling to avoid infinite loops.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/csharp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">C# Fundamentals (Beginner Level) > Loops > 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-1779288309903\"><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":17,"template":"","class_list":["post-33","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>while Loop - Learn C# Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.\" \/>\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\/csharp\/?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 - Learn C# Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/csharp\/?lesson=while-loop\" \/>\n<meta property=\"og:site_name\" content=\"Learn C# Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-20T17:33:19+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\\\/csharp\\\/?lesson=while-loop\",\"url\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/?lesson=while-loop\",\"name\":\"while Loop - Learn C# Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/#website\"},\"datePublished\":\"2026-05-20T14:45:49+00:00\",\"dateModified\":\"2026-05-20T17:33:19+00:00\",\"description\":\"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/?lesson=while-loop#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/csharp\\\/?lesson=while-loop\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/?lesson=while-loop#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/csharp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Fundamentals (Beginner Level) > Loops > while Loop\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/\",\"name\":\"Learn C# Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/csharp\\\/?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 - Learn C# Language with GiGz.PK","description":"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.","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\/csharp\/?lesson=while-loop","og_locale":"en_US","og_type":"article","og_title":"while Loop - Learn C# Language with GiGz.PK","og_description":"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.","og_url":"https:\/\/gigz.pk\/csharp\/?lesson=while-loop","og_site_name":"Learn C# Language with GiGz.PK","article_modified_time":"2026-05-20T17:33:19+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\/csharp\/?lesson=while-loop","url":"https:\/\/gigz.pk\/csharp\/?lesson=while-loop","name":"while Loop - Learn C# Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/csharp\/#website"},"datePublished":"2026-05-20T14:45:49+00:00","dateModified":"2026-05-20T17:33:19+00:00","description":"Learn C# while loop with syntax and examples. Understand repetition and conditions with simple beginner-friendly explanation.","breadcrumb":{"@id":"https:\/\/gigz.pk\/csharp\/?lesson=while-loop#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/csharp\/?lesson=while-loop"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/csharp\/?lesson=while-loop#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/csharp"},{"@type":"ListItem","position":2,"name":"C# Fundamentals (Beginner Level) > Loops > while Loop"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/csharp\/#website","url":"https:\/\/gigz.pk\/csharp\/","name":"Learn C# Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/csharp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/csharp\/index.php?rest_route=\/wp\/v2\/lesson\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/csharp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/csharp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/csharp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}