{"id":184,"date":"2021-12-24T14:14:43","date_gmt":"2021-12-24T07:14:43","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=184"},"modified":"2023-07-02T19:09:30","modified_gmt":"2023-07-02T12:09:30","slug":"csharp-do-while","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-do-while\/","title":{"rendered":"C# do while"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# <code>do while<\/code> statement to execute a block of code repeatedly as long a condition is <code>true<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# do while statement<\/h2>\n\n\n\n<p>The C# <code>do while<\/code> statement executes one or more iterations as long as a condition is <code>true<\/code>.<\/p>\n\n\n\n<p>The following shows the syntax of the <code>do while<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">do<\/span>\n{\n    <span class=\"hljs-comment\">\/\/ statement<\/span>\n} <span class=\"hljs-keyword\">while<\/span> (expression);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Unlike the <code><a href=\"https:\/\/csharptutorial.net\/csharp-tutorial\/csharp-while\/\">while<\/a><\/code> statement, the <code>do while<\/code> statement checks the <code>expression<\/code> at the end of each iteration. Therefore, it&#8217;s called a posttest loop.<\/p>\n\n\n\n<p>The <code>do while<\/code> statement will always run the first iteration regardless of the expression&#8217;s result. And it&#8217;ll execute the block repeatedly as long as the expression is <code>true<\/code>.<\/p>\n\n\n\n<p>Inside the loop, you need to change some variables to make the expression <code>false<\/code> so that the loop will terminate after some iterations. Otherwise, you&#8217;ll have an indefinite loop.<\/p>\n\n\n\n<p>The following flowchart illustrates how the <code>do while<\/code> 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-do-while.svg\" alt=\"C# do while\" class=\"wp-image-539\"\/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">C# do while statement examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>do while<\/code> statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Simple C# do while statement example<\/h3>\n\n\n\n<p>The following example shows how to use the <code>do while<\/code> statement to print out five numbers from 1 to 5:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">int counter = <span class=\"hljs-number\">0<\/span>;\n<span class=\"hljs-keyword\">do<\/span>\n{\n    counter++;\n    Console.WriteLine(counter);\n\n} <span class=\"hljs-keyword\">while<\/span> (counter &lt; <span class=\"hljs-number\">5<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">1\n2\n3\n4\n5<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare the <code>counter<\/code> variable and initialize its value to 0.<\/p>\n\n\n\n<p>Second, increase the <code>counter<\/code> by one and display it in the <code>do while<\/code> block. Repeat this step as long as the <code>counter<\/code> is less than 5. Since the <code>counter<\/code> starts at one, the <code>do while<\/code> statement executes exactly five times.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the do-while statement to create a guessing game<\/h3>\n\n\n\n<p>The following illustrates how to use the <code>do while<\/code> statement to create the &#8220;Guess the Number&#8221; game. The program will generate a random number between 1 and 10. <\/p>\n\n\n\n<p>And you&#8217;ll have a maximum of 4 turns. If your guess is higher or lower than a secret number, you&#8217;ll get a hint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">int guess = <span class=\"hljs-number\">0<\/span>, yourNumber;\nstring hint;\n\n\n<span class=\"hljs-comment\">\/\/ Get a random number between 1 and 10<\/span>\nint secretNumber = (<span class=\"hljs-keyword\">new<\/span> Random()).Next(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>);\n\nConsole.WriteLine(<span class=\"hljs-string\">\"Pick a number between 1 and 10. You'll have 4 turns.\"<\/span>);\n<span class=\"hljs-keyword\">do<\/span>\n{\n    guess++;\n\n    <span class=\"hljs-comment\">\/\/ Get the user input<\/span>\n    Console.Write($<span class=\"hljs-string\">\"Turn #{guess}. Your number:\"<\/span>);\n    yourNumber = Convert.ToInt32(Console.ReadLine());\n\n    <span class=\"hljs-comment\">\/\/ Check the input number with the secret number<\/span>\n    <span class=\"hljs-keyword\">if<\/span> (yourNumber &lt; secretNumber)\n    {\n        hint = $<span class=\"hljs-string\">\"Your guess: {yourNumber}, which is too low.\"<\/span>;\n    }\n    <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (yourNumber &gt; secretNumber)\n    {\n        hint = $<span class=\"hljs-string\">\"Your guess: {yourNumber}, which is too high.\"<\/span>;\n    }\n    <span class=\"hljs-keyword\">else<\/span>\n    {\n        hint = $<span class=\"hljs-string\">\"Bingo! It took you {guess} turns to guess the secret number {secretNumber}.\"<\/span>;\n    }\n    Console.WriteLine(hint);\n\n} <span class=\"hljs-keyword\">while<\/span> (guess &lt; <span class=\"hljs-number\">4<\/span> &amp;&amp; yourNumber != secretNumber);\n\n\n<span class=\"hljs-keyword\">if<\/span> (yourNumber != secretNumber)\n{\n    Console.WriteLine(<span class=\"hljs-string\">\"Oops! you lost.\"<\/span>);\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, declare the <code>guess<\/code> variable to store the number of guesses and <code>yourNumber<\/code> variable to store the input number:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">int guess = 0, yourNumber;<\/code><\/span><\/pre>\n\n\n<p>Second, get a random integer between 1 and 10:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">int secretNumber = (<span class=\"hljs-keyword\">new<\/span> Random()).Next(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This code creates a new instance of the <code>Random<\/code> class and call the <code>Next()<\/code> method to generate a random number between 1 and 10. Note that you&#8217;ll learn more about classes and methods in the later tutorial.<\/p>\n\n\n\n<p>Third, show the message on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Console.WriteLine(<span class=\"hljs-string\">\"Pick a number between 1 and 10. You'll have 4 turns.\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, use the <code>do while<\/code> statement to get the user&#8217;s input number. <\/p>\n\n\n\n<p>The loop will continue if the number of guesses is less than 4 and the input number is different from the secret number. <\/p>\n\n\n\n<p>Inside the <code>do while<\/code> loop:<\/p>\n\n\n\n<p>1) Increase the value of the <code>guess<\/code> variable by one in each iteration:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">guess++;<\/code><\/span><\/pre>\n\n\n<p>2) Get the input number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Console.Write($<span class=\"hljs-string\">\"Turn #{guess}. Your number:\"<\/span>);\nyourNumber = Convert.ToInt32(Console.ReadLine());<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>3) Check the input number with the secret number and give a hint if the input number is lower or higher. Also, print a message if the input number equals the secret number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> (yourNumber &lt; secretNumber)\n{\n    Console.WriteLine($<span class=\"hljs-string\">\"Your guess: {yourNumber}, which is too low.\"<\/span>);\n}\n<span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (yourNumber &gt; secretNumber)\n{\n    Console.WriteLine($<span class=\"hljs-string\">\"Your guess: {yourNumber}, which is too high.\"<\/span>);\n}\n<span class=\"hljs-keyword\">else<\/span>\n{\n    Console.WriteLine($<span class=\"hljs-string\">\"Bingo! It took you {guess} turns to guess the secret number {secretNumber}.\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, print a message if the input number is not equal to the secret number:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> (yourNumber != secretNumber)\n{\n    Console.WriteLine(<span class=\"hljs-string\">\"Oops! you lost.\"<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following shows a test:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Pick a number between 1 and 10. You'll have 4 turns.\nTurn #1. Your number:5\nYour guess: 5, which is too low.\nTurn #2. Your number:6\nYour guess: 6, which is too low.\nTurn #3. Your number:7\nYour guess: 7, which is too low.\nTurn #4. Your number:8\nBingo! It took you 4 turns to guess the secret number 8.<\/code><\/span><\/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>do while<\/code> statement to execute a block of code repeatedly as long as a condition is true.<\/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=\"184\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-do-while\/\"\n\t\t\t\tdata-post-title=\"C# do 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=\"184\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-do-while\/\"\n\t\t\t\tdata-post-title=\"C# do 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# do while statement to execute a block of code repeatedly as long a condition is true. Introduction to the C# do while statement The C# do while statement executes one or more iterations as long as a condition is true. The following shows the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-184","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/184","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=184"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/184\/revisions"}],"predecessor-version":[{"id":2526,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/184\/revisions\/2526"}],"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=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}