{"id":178,"date":"2021-12-24T11:17:21","date_gmt":"2021-12-24T04:17:21","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=178"},"modified":"2023-07-02T19:06:07","modified_gmt":"2023-07-02T12:06:07","slug":"csharp-while","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-while\/","title":{"rendered":"C# while"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# <code>while<\/code> statement to execute a block while a boolean expression is <code>true<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# while statement<\/h2>\n\n\n\n<p>The <code>while<\/code> statement evaluates a boolean expression and executes a block repeatedly as long as the expression is <code>true<\/code>. Here&#8217;s the syntax of the <code>while<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">while<\/span> (expression)\n{\n    <span class=\"hljs-comment\">\/\/ statement<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>The <code>expression<\/code>, which follows the <code>while<\/code> keyword, must be a boolean expression that evaluates to <code>true<\/code> or <code>false<\/code>.<\/p>\n\n\n\n<p>The <code>while<\/code> statement evaluates the <code>expression<\/code> first. If the <code>expression<\/code> evaluates to <code>true<\/code>, it&#8217;ll execute the block inside the curly braces.<\/p>\n\n\n\n<p>Once completed executing the block, the <code>while<\/code> statement checks the <code>expression<\/code> again. And it&#8217;ll execute the block again as long as the <code>expression<\/code> is <code>true<\/code>.<\/p>\n\n\n\n<p>If the <code>expression<\/code> is <code>false<\/code>, the <code>while<\/code> statement exits and passes the control to the statement after it.<\/p>\n\n\n\n<p>Therefore, you need to change some <a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-variables\/\">variables<\/a> inside the block to make the <code>expression<\/code> <code>false<\/code> at some point. Otherwise, you&#8217;ll have an <strong>indefinite loop<\/strong>.<\/p>\n\n\n\n<p>Since the <code>expression<\/code> is checked at the beginning of each iteration, the <code>while<\/code> statement is often called a pretest loop.<\/p>\n\n\n\n<p>The following flowchart illustrates how the C# while statement works.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/csharptutorial.net\/wp-content\/uploads\/2022\/01\/csharp-while.svg\" alt=\"c# while\" class=\"wp-image-535\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">C# while statement examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>while<\/code> statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple C# while statement example<\/h3>\n\n\n\n<p>The following example uses the <code>while<\/code> loop statement to output five numbers from 1 to 5 to the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">int<\/span> counter = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">while<\/span>(counter &lt; <span class=\"hljs-number\">5<\/span>)\n{\n    counter++;\n    Console.WriteLine(counter);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-number\">3<\/span>\n<span class=\"hljs-number\">4<\/span>\n<span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare a <code>counter<\/code> variable and initialize it to zero.<\/p>\n\n\n\n<p>Second, enter the <code>while<\/code> loop because the following expression is <code>true<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">counter &lt; <span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, increase the <code>counter<\/code> by one and print it out to the console; repeat this step as long as the <code>counter<\/code> is less than 5. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the C# while statement to calculate the average<\/h3>\n\n\n\n<p>The following program prompts users to enter a list of numbers and calculate the average:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">double<\/span> number = <span class=\"hljs-number\">0<\/span>,\n    total = <span class=\"hljs-number\">0<\/span>,\n    count = <span class=\"hljs-number\">0<\/span>,\n    average = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">string<\/span> input = <span class=\"hljs-string\">\"\"<\/span>;\n\n\nConsole.WriteLine(<span class=\"hljs-string\">\"Enter a list of numbers to calculate the average (Q - quit):\"<\/span>);\n\n<span class=\"hljs-keyword\">while<\/span> (input != <span class=\"hljs-string\">\"Q\"<\/span> &amp;&amp; input != <span class=\"hljs-string\">\"q\"<\/span>)\n{\n    input = Console.ReadLine();\n    <span class=\"hljs-keyword\">if<\/span> (input != <span class=\"hljs-string\">\"Q\"<\/span> &amp;&amp; input != <span class=\"hljs-string\">\"q\"<\/span>)\n    {\n        number = Convert.ToDouble(input);\n        total += number;\n        count++;\n    }\n}\n\n<span class=\"hljs-keyword\">if<\/span> (count &gt; <span class=\"hljs-number\">0<\/span>)\n{\n    average = total \/ count;\n}\n\nConsole.WriteLine(<span class=\"hljs-string\">$\"Average:<span class=\"hljs-subst\">{average}<\/span>\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare variables and initialize them:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">double<\/span> number = <span class=\"hljs-number\">0<\/span>,\n    total = <span class=\"hljs-number\">0<\/span>,\n    count = <span class=\"hljs-number\">0<\/span>,\n    average = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">string<\/span> input = <span class=\"hljs-string\">\"\"<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, print out the instruction:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Console.WriteLine(<span class=\"hljs-string\">\"Enter a list of numbers to calculate the average (Q - quit):\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, prompt users to enter a number until they enter the letter <code>Q<\/code> or <code>q<\/code>. In each iteration, calculate the total and count the entered numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">while<\/span> (input != <span class=\"hljs-string\">\"Q\"<\/span> &amp;&amp; input != <span class=\"hljs-string\">\"q\"<\/span>)\n{\n    input = Console.ReadLine();\n    <span class=\"hljs-keyword\">if<\/span> (input != <span class=\"hljs-string\">\"Q\"<\/span> &amp;&amp; input != <span class=\"hljs-string\">\"q\"<\/span>)\n    {\n        number = Convert.ToDouble(input);\n        total += number;\n        count++;\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, calculate the average if users enter at least one number and output it to the console:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">if<\/span> (count &gt; <span class=\"hljs-number\">0<\/span>)\n{\n    average = total \/ count;\n}\nConsole.WriteLine(<span class=\"hljs-string\">$\"Average:<span class=\"hljs-subst\">{average}<\/span>\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the following output, we enter three numbers 10, 20, and 30. And the program shows the average as 20:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\">Enter a list of numbers to calculate the <span class=\"hljs-title\">average<\/span> (<span class=\"hljs-params\">Q - quit<\/span>):\n10\n20\n30\nq\nAverage:20<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>while<\/code> statement to execute a block as long as a boolean expression is <code>true<\/code>.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"178\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-while\/\"\n\t\t\t\tdata-post-title=\"C# while\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"178\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-while\/\"\n\t\t\t\tdata-post-title=\"C# while\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the C# while statement to execute a block while a boolean expression is true. Introduction to the C# while statement The while statement evaluates a boolean expression and executes a block repeatedly as long as the expression is true. Here&#8217;s the syntax of the while statement: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":16,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-178","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/178\/revisions"}],"predecessor-version":[{"id":2525,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/178\/revisions\/2525"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}