{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "99c333cf-ec76-4ee7-981b-ce6282d28fbc",
   "metadata": {},
   "source": [
    "```{index} while; statement\n",
    "```\n",
    "\n",
    "(while-statements)=\n",
    "(dangerous-semicolon)=\n",
    "\n",
    "# While-Statements\n",
    "\n",
    "Just like the `for` loop, the while loop deals with iteration (*repetition* or *loops*).\n",
    "The `while` loop is **condition-controlled**, as opposing to `for` loop , which is\n",
    "**count-controlled**. This means a while loop runs a set of instructions continuously\n",
    "as long as the given **boolean condition** evaluates true, no matter how many loops it takes.\n",
    "Only when the condition is evaluated to NOT true, then the loop terminates.\n",
    "\n",
    "The flowchart of a while loop looks like the graph below, note that\n",
    "the sequential flow of a program is altered with the loops, just like with method calls\n",
    "and conditional statements: [1]\n",
    "\n",
    ":::{figure} ../../images/flowchart-while-loop.jpg\n",
    ":alt: flowchart while loop\n",
    ":width: 250\n",
    ":::\n",
    "\n",
    "For `while` loop, it is common to initialize a variable to be used in the\n",
    "condition, therefore the syntax would look like:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "mystnb": {
     "syntax_highlight_language": "none"
    }
   },
   "outputs": [],
   "source": [
    "initialization\n\nwhile ( *condition* )\n{\n// statement(s)\n// iterator expression\n}"
   ],
   "id": "d4236cd5"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n\nNote that, because the condition (boolean expression) is evaluated before each execution\nof the loop body (#4-6), a while loop can executes **zero** (if the condition\nevaluates to false immediately) or more times.\n\nYou should test the following code, less the comments, in `csharprepl` to get familiar with\nthe C# while loop:\n\nint n = 0; // initialization\n\nwhile (n < 5) // conditional expression in the while loop header\n{\nConsole.Write(n); // body of while loop\nn++; // iterator (iterator expression)/update\n}\n\n// output: 01234\n>\n\n\nNote that this `while` loop code above is just the same as a `for` loop:\n\nfor (int i = 0; i < 5; i++)\n{\nConsole.Write(i);\n}\n\n// output: 01234\n\n\nBoth code have `initializer`, `conditional expression`, and `iterator expression`\n( see {ref}`for-loop-header`); except that in `for loop`, the three sections are\n"
   ],
   "id": "a164035a"
  },
  {
   "cell_type": "markdown",
   "id": "2fc12ac4",
   "metadata": {},
   "source": [
    "placed together in the header, while they are placed in different locations in\n",
    "    the `while` loop.\n",
    "\n",
    "    **Stepping**\n",
    "\n",
    "    Test yourself: Follow the code below to figure out what is printed:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int i = 4;\n",
    "    while (i < 9)\n",
    "    {\n",
    "       Console.WriteLine(i);\n",
    "       i = i + 2;\n",
    "    }\n",
    "\n",
    "Compare the preceding code to the code loop below:\n"
   ],
   "id": "81418abb"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int i = 4; while (i < 9)\n",
    "{\n",
    "i = i + 2;\n",
    "Console.WriteLine(i);\n",
    "}\n",
    "\n",
    "\n",
    "    Do they produce the same result? Now, compare the preceding code with the following:\n"
   ],
   "id": "b01a84fd"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    > for (int i = 4; i < 9; i += 2)\n",
    "      {\n",
    "          Console.WriteLine(i);\n",
    "      }\n",
    "\n",
    "Make sure you are able to interpret the code correctly and type them in csharprepl or\n",
    "VS Code to test them out.\n"
   ],
   "id": "d9770352"
  },
  {
   "cell_type": "markdown",
   "id": "9af099bc",
   "metadata": {},
   "source": "## Infinite loops\n\nJust like {ref}`for-statement`, manipulating the header sections will\nchange the behavior of the loop. Test the following code in your `csharprepl` and be ready\nto issue `Control + C` to terminate the process:\n\n    > int n = 0;\n    > while (n < 5)\n    {\n       Console.Write(n);\n    }\n    000000000000000000000000000000...\n\nObserving the code, you see that the variable `n` is not being updated in the body\nof the while loop. Since `n` is not updated, the value stays as 0, and the boolean\ncondition `(n < 5>)` is always evaluated to be `true`, an infinite loop is therefore\nformed since (n \\< 5) will stay true and, while the boolean condition is tested true, the\nbody of the while loop will be executed and print out `n`.\n\nIf you want the while loop body of the while statement to run at least once, the boolean\ncondition has to be true for the first evaluation. After that, the iterator (e.g., n++)\nin the body of the while statement needs to work to exit the loop by making the condition\nsection untrue. The preceding code does not have an iterator expression and therefore\nthe loop becomes infinite.\n\nFor your possible interest, you may want to test the following `for` statement. Again,\nbe ready to issue `Control + C` to terminate the process:\n\n    > for (int i = 0; i < 5;)\n      {\n          Console.Write(i);\n      }\n\nAfter testing the code above, you should get better idea about how boolean expressions\ncontrols the code execution in loop.\n\n**while (true)**\n\nAs an exercise, observe the following code. You should be able to see that the condition\nsection has a value of `true` instead of an expression and reason the outcome of the\ncode:\n\n    > while (true)\n      {\n          Console.Write(0);\n      }\n\nAn infinite loop can happen when:\n\n1.  The loop has no terminating condition.\n2.  The loop has a terminating condition that cannot be met.\n\nAn embedded system such as a cartridge-based video game console typically does not\nhave an `exit` condition and the loop runs until the console is powered off. The\nsame infinite loop design can be seen in operating systems or web servers, where the\nsystems keep monitor input and give output and do not halt until crash, turned off,\nor reset.\n\n`{rubric} Footnotes`\n\n[1] The flowchart is from [geeksforgeeks.org](https://www.geeksforgeeks.org/c-while-loop/#)"
  },
  {
   "cell_type": "markdown",
   "id": "c74f7823",
   "metadata": {},
   "source": [
    "## While Examples\n",
    "\n",
    "The following examples were consolidated from `0602-while-examples`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2eb0e52f",
   "metadata": {},
   "source": "## String Operations\n\nThe `string` type represents a sequence of zero or more Unicode characters\n(the `char` type) with indices. We can therefore access the characters in a\nstring using array notation with index values. Consider the following method:\n\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "using System;\n",
    "namespace IntroCSCS\n",
    "{\n",
    "   class Ch06WhileLoop\n",
    "   {\n",
    "\n",
    "      static void Main()\n",
    "      {\n",
    "         Console.Write(\"Please enter a string: \");\n",
    "         string s = Console.ReadLine();\n",
    "         OneCharPerLine(s);\n",
    "      }\n",
    "\n",
    "      static void OneCharPerLine(string s)\n",
    "      {\n",
    "         int i = 0;\n",
    "         while (i < s.Length) {\n",
    "            Console.WriteLine(s[i]);\n",
    "            i++;\n",
    "         }\n",
    "      }\n",
    "   }\n",
    "}\n"
   ],
   "id": "05022464"
  },
  {
   "cell_type": "markdown",
   "id": "e037c960",
   "metadata": {},
   "source": [
    "\n",
    "We are using `(i < s.Length)` because we want to loop through all the characters.\n",
    "Since array indexing is 0-based, the index of the last character is the\n",
    "*length of the string - 1*. In the code here, it is s.Length - 1. To represent this\n",
    "number, we use:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while (i < s.Length) {\n"
   ],
   "id": "c5942180"
  },
  {
   "cell_type": "markdown",
   "id": "1dc687d2",
   "metadata": {},
   "source": [
    "\n",
    "To print out the characters, we loop through the string use the array indices `s[i]`:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Console.WriteLine(s[i]);\n"
   ],
   "id": "56642cc8"
  },
  {
   "cell_type": "markdown",
   "id": "e4c7ec34",
   "metadata": {},
   "source": [
    "\n",
    "To print out the characters one by one, we update the local loop variable `i`\n",
    "with step of 1:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = i+1;\n"
   ],
   "id": "0b8a605c"
  },
  {
   "cell_type": "markdown",
   "id": "cac7fe93",
   "metadata": {},
   "source": [
    "\n",
    "or the more commonly used equivalent form:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i++;\n"
   ],
   "id": "1d772d0c"
  },
  {
   "cell_type": "markdown",
   "id": "3ff38358",
   "metadata": {},
   "source": "```{index} exercise; savings\n```\n\n(savings-exercise)=\n\n## Long Term Investment\n\nThe idea here is to see how many years it will take for an investment account to\ngrow to at least a given value, assuming a compounded hypothetical annual return\nrate. Here's what we will do:\n\n1. Prompts the user for three numbers:\n\n   1. The monthly investment.\n   2. The annual percentage for return as a decimal. For index funds (mutual or ETF,\n      Exchange Traded Funds), it seems to be reasonable to have annualized 5% return.\n   3. The final balance desired.\n\n#. Print the initial balance, 0, and the balance each year until\nthe desired amount is reached. Round displayed amounts\nto two decimal places, as usual.\n\nThe math: The amount next year is the amount now times\n(1 + interest fraction),\nso if I have \\$500 now and the interest rate is .04,\nI have \\$500\\*(1.04) = \\$520 after one year, and after two years I have,\n\\$520\\*(1.04) = \\$540.80.\nIf I enter into the program a \\$500 starting balance, .04 interest rate and\na target of \\$550, the program prints:\n\n```\n500.00\n520.00\n540.80\n563.42\n```"
  },
  {
   "cell_type": "markdown",
   "id": "5dc9b677",
   "metadata": {},
   "source": "```{index} exercise; strange sequence\n```\n\n(strange-seq-ex)=\n\n### Strange Sequence Exercise\n\nSave the example program [strange_seq_stub/strange_seq.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/strange_seq_stub/strange_seq.cs)\nin a project of your own.\n\nThere are three functions to complete. Do one at a time and test.\n\n`Jump`: First complete the definitions of function `Jump`.\nFor any integer `n`, `Jump(n)` is `n/2` if `n` is even,\nand `3*n+1` if `n` is odd.\nIn the `Jump` function definition use an {{ if_else }}\nstatement. Hint [^oddeven]\n\n`PrintStrangeSequence`:\nYou can start with one number, say n = 3, and *keep* applying the\n`Jump` function to the *last* number given,\nand see how the numbers jump around!"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Jump(3) = 3*3+1 = 10; Jump(10) = 10/2 = 5;\n",
    "Jump(5) = 3*5+1 = 16; Jump(16) = 16/2 = 8;\n",
    "Jump(8) = 8/2  =   4; Jump(4) =   4/2 = 2;\n",
    "Jump(2) = 2/2  =   1\n"
   ],
   "id": "94360009"
  },
  {
   "cell_type": "markdown",
   "id": "2d50dd55",
   "metadata": {},
   "source": "\nThis process of repeatedly applying the same function to the most recent result\nis called function *iteration*. In this case you see that iterating the\n`Jump` function, starting from n=3, eventually reaches the value 1.\n\nIt is an *open research question* whether iterating the Jump function\nfrom an integer `n` will eventually reach 1,\nfor *every* starting integer `n` greater than 1.\nResearchers have only found examples of `n` where it is true.\nStill, no general argument has been made to apply to the\n*infinite* number of possible starting integers.\n\nIn the PrintStrangeSequence you iterate the `Jump` function\nstarting from parameter value `n`, as long as the current number is not 1.\nIf you start with 1, stop immediately.\n\n`CountStrangeSequence`: Iterate the `Jump` function as in\n`PrintStrangeSequence`. Instead of printing each number in the sequence,\njust count them, and return the count.\n\n% later - sequence of counts?\n% After you have finished and saved ``jump_seq.cs`` copy it and save\n%     the file as ``jump_seq_lengths.cs``.\n%\n%     First modify the main method so it prompts the user\n%     for a value of n, and then prints just the length of the iterative sequence\n%     from listJumps(n).  Hint [#]_\n%\n%     Then elaborate the program so it prompts the user for two integers:\n%     a lowest starting value of n\n%     and a highest starting value of n.\n%     For all integers n in the range from the lowest start through\n%     the highest start, including the highest,\n%     print a sentence giving the starting value of n\n%     and the length of the list from ``listJumps(n)``.  An example run::\n%\n%             Enter lowest start: 3\n%             Enter highest start: 6\n%             Starting from 3, Jump sequence length 8.\n%             Starting from 4, Jump sequence length 3.\n%             Starting from 5, Jump sequence length 6.\n%             Starting from 6, Jump sequence length 9."
  },
  {
   "cell_type": "markdown",
   "id": "e8d0869a",
   "metadata": {},
   "source": "```{index} exercise; roundoff II\n```\n\n(roundoff2)=\n\n### Roundoff Exercise II\n\nWrite a program to complete and test the function with this heading\nand documentation:"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/// Return the largest possible number y, so in C#: x+y = x\n",
    "/// If x is Infinity return Infinity.\n",
    "/// If x is -Infinity, return double.MaxValue.\n",
    "/// Assume x is not NaN (which is equal to nothing).\n",
    "static double Epsilon(double x)\n"
   ],
   "id": "13596734"
  },
  {
   "cell_type": "markdown",
   "id": "91d8917c",
   "metadata": {},
   "source": [
    "\n",
    "Hint: The non-exceptional case can have some similarity\n",
    "to the bisection in the best root approximation example:\n",
    "start with two endpoints, `a` and `b`, where `x+a = x` and\n",
    "`x+b > x`, and reduce the interval size by half....\n",
    "\n",
    "[^oddeven]: If you divide an even number by 2, what is the remainder? Use this idea\n",
    "    in your `if` condition."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd78184a",
   "metadata": {},
   "source": [
    "## Do-While Loops\n",
    "\n",
    "The following content was consolidated from `0603-do-while`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4da9bd0a-d492-4745-81ec-ac4444ab7a14",
   "metadata": {},
   "source": [
    "```{index} statement; do while\n",
    "```\n",
    "\n",
    "(do-while)=\n",
    "\n",
    "# Do-While Loops\n",
    "\n",
    "The general form of a {{ do_while }} statement (referred to as the “`do` statement” in\n",
    "the C# language reference [^1]) is:\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa3284d3",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "source": [
    "```csharp\n",
    "initialization\n",
    "\n",
    "do\n",
    "{\n",
    "  // statement(s)\n",
    "  // iterator expression\n",
    "} while ( condition );\n",
    "\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c02162c",
   "metadata": {},
   "source": [
    "The `do-while` loop is a variation of the `while loop`. The `do` statement executes a statement or a block of statements, followed by a Boolean expression to decide the continuation of the iteration. Because that expression is evaluated **after** each execution of the loop, a do loop executes **one or more** times. The `do` loop differs from the *while* loop, which executes *zero or more* times.\n",
    "\n",
    "Compare the `do-while` general syntax with the `while` loop below and you should see clearly the difference in logic.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ac15d58",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "source": [
    "```csharp\n",
    "initialization\n",
    "\n",
    "while ( *condition* )\n",
    "{\n",
    "  // statement(s)\n",
    "  // iterator expression\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a `while` loop, if the condition evaluates to false from\n",
    "the very beginning, the body of statements will not be executed at all.\n",
    "In a `do-while` statement, the body statement(s) is *always* executed at least\n",
    "once, then the iteration continues if the followed condition tested true.\n",
    "This logic can be seen in the flowchart:"
   ],
   "id": "a369305d"
  },
  {
   "cell_type": "markdown",
   "id": "78549cf7",
   "metadata": {},
   "source": [
    "```{figure} ../../images/flowchart-do-while-loop.jpg\n",
    ":alt: flowchart do-while loop\n",
    ":width: 250\n",
    "Flowchart of a do-while loop.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f371407a",
   "metadata": {},
   "source": [
    "As a comparison, the while and do-while loops differ in several ways:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "294e6420",
   "metadata": {},
   "source": [
    "| Feature | while | do-while |\n",
    "| --- | --- | --- |\n",
    "| Condition | checked before body statement is executed. | checked after body statement is executed. |\n",
    "| Body statement | may be executed zero times. | is executed at least once. |\n",
    "| Semicolon | no semicolon at the end of loop header: `while(condition)` | semicolon at the end of loop header: `while(condition);` |\n",
    "| Loop Variable | initialized before the execution of loop. | initialized before or within the loop. |\n",
    "| Control | entry-controlled. | exit-controlled. |\n",
    "| Syntax | `while(condition) { statement(s); }` | `do { statement(s); } while(condition);` |\n",
    "\n",
    "To get familiar with the do-while loop syntax, you should practice the following code in `csharprepl`:\n",
    "\n",
    "```csharp\n",
    "int n = 0;\n",
    "do\n",
    "{\n",
    "  Console.Write(n);\n",
    "  n++;\n",
    "} while (n < 5);\n",
    "\n",
    "// output: 01234\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d2fd44e",
   "metadata": {},
   "source": "## `do-while` Example: Right Triangle\n\nSuppose you want the user to enter three integers for sides of a right triangle. If they do not make a right triangle, say so and make the user try again. One way to look at the while statement rubric is:\n\n```csharp\nset data for condition\nwhile (condition) {\n   accomplish something\n   set data for condition\n}\n```\n\n**Using input methods in the UI class**: In this example, you will solicit user input for three times. While you may do so by using three Console.Write() and Console.ReadLine() lines to save the user input data, you may also try using the customized user input methods in the UI class (for how to use\nthe methods, see {ref}`the-ui-class`).\n\nIn cases like this, we would like to intake user data, perform certain operations to the data that are too complicated for the while header condition, then make the decision about the iteration. A {{ do_while }} loop works better because we want to collect user input first, perform some calculation, before we tell the user that their input is correct or not:"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2331b6a2",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [
    {
     "ename": "Error",
     "evalue": "(4,9): error CS0103: The name 'UI' does not exist in the current context\n(5,9): error CS0103: The name 'UI' does not exist in the current context\n(6,9): error CS0103: The name 'UI' does not exist in the current context",
     "output_type": "error",
     "traceback": [
      "(4,9): error CS0103: The name 'UI' does not exist in the current context\n",
      "(5,9): error CS0103: The name 'UI' does not exist in the current context\n",
      "(6,9): error CS0103: The name 'UI' does not exist in the current context"
     ]
    }
   ],
   "source": [
    "int a, b, c;\n",
    "do {\n",
    "    Console.WriteLine(\"Think of integer sides for a right triangle.\");\n",
    "    a = UI.PromptInt(\"Enter integer leg: \");\n",
    "    b = UI.PromptInt(\"Enter another integer leg: \");\n",
    "    c = UI.PromptInt(\"Enter integer hypotenuse: \");\n",
    "    if (a*a + b*b != c*c) {\n",
    "        Console.WriteLine(\"Not a right triangle: Try again!\");\n",
    "    }\n",
    "} while (a*a + b*b != c*c);"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54c9123f",
   "metadata": {},
   "source": [
    "A {{ do_while }} loop is the *one* place where you *do* want a semicolon\n",
    "right after a condition, unlike the places mentioned in\n",
    "{ref}`dangerous-semicolon`. At least if you omit it here you\n",
    "are likely to get a compiler error rather than a difficult logical\n",
    "bug.\n",
    "\n",
    "A {{ do_while }} loop, like the example above,\n",
    "can accomplish exactly the same thing as the `while`\n",
    "loop rubric at the beginning of this section. It has the general form:\n",
    "\n",
    "``` none\n",
    "do {\n",
    "   set data for condition\n",
    "   if (condition) {\n",
    "       accomplish something\n",
    "   }\n",
    "} while (condition);\n",
    "```\n",
    "\n",
    "In the example above note that the declaration of `a`, `b`, and `c` is\n",
    "*before* the {{ do_while }}\n",
    "loop. You can try moving the declaration inside the braces for the loop body,\n",
    "and see the compiler error that you get!\n",
    "\n",
    "Recall the variables declared inside\n",
    "a braces-delimited block have scope *local to that block*. The condition at\n",
    "the end of the loop is *outside* that scope. Hence the declaration of variables that\n",
    "you want in the final test or for later use after the loop must be\n",
    "declared *before* the {{ do_while }} loop."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db73bc91",
   "metadata": {},
   "source": [
    "```{rubric} Footnotes\n",
    "```\n",
    "\n",
    "[^1]: For an introduction of the while loop, see: [ the while loop in C# Language Reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/statements#1392-the-while-statement)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(",
    "g",
    "c",
    "d",
    ")",
    "=",
    "\n",
    "\n",
    "#",
    "#",
    " ",
    "G",
    "C",
    "D",
    " ",
    "E",
    "x",
    "a",
    "m",
    "p",
    "l",
    "e",
    " ",
    "w",
    "i",
    "t",
    "h",
    " ",
    "a",
    " ",
    "`",
    "w",
    "h",
    "i",
    "l",
    "e",
    "`",
    " ",
    "L",
    "o",
    "o",
    "p",
    "\n",
    "\n",
    "A",
    " ",
    "c",
    "o",
    "m",
    "p",
    "a",
    "c",
    "t",
    " ",
    "a",
    "n",
    "d",
    " ",
    "p",
    "r",
    "a",
    "c",
    "t",
    "i",
    "c",
    "a",
    "l",
    " ",
    "l",
    "o",
    "o",
    "p",
    " ",
    "e",
    "x",
    "a",
    "m",
    "p",
    "l",
    "e",
    " ",
    "i",
    "s",
    " ",
    "E",
    "u",
    "c",
    "l",
    "i",
    "d",
    "'",
    "s",
    " ",
    "a",
    "l",
    "g",
    "o",
    "r",
    "i",
    "t",
    "h",
    "m",
    " ",
    "f",
    "o",
    "r",
    " ",
    "t",
    "h",
    "e",
    " ",
    "g",
    "r",
    "e",
    "a",
    "t",
    "e",
    "s",
    "t",
    " ",
    "c",
    "o",
    "m",
    "m",
    "o",
    "n",
    " ",
    "d",
    "i",
    "v",
    "i",
    "s",
    "o",
    "r",
    " ",
    "(",
    "G",
    "C",
    "D",
    ")",
    ":",
    "\n",
    "\n",
    "-",
    " ",
    "C",
    "o",
    "n",
    "t",
    "i",
    "n",
    "u",
    "e",
    " ",
    "w",
    "h",
    "i",
    "l",
    "e",
    " ",
    "`",
    "b",
    " ",
    "!",
    "=",
    " ",
    "0",
    "`",
    ".",
    "\n",
    "-",
    " ",
    "C",
    "o",
    "m",
    "p",
    "u",
    "t",
    "e",
    " ",
    "r",
    "e",
    "m",
    "a",
    "i",
    "n",
    "d",
    "e",
    "r",
    " ",
    "`",
    "r",
    " ",
    "=",
    " ",
    "a",
    " ",
    "%",
    " ",
    "b",
    "`",
    ".",
    "\n",
    "-",
    " ",
    "S",
    "h",
    "i",
    "f",
    "t",
    " ",
    "v",
    "a",
    "l",
    "u",
    "e",
    "s",
    ":",
    " ",
    "`",
    "a",
    " ",
    "=",
    " ",
    "b",
    "`",
    ",",
    " ",
    "`",
    "b",
    " ",
    "=",
    " ",
    "r",
    "`",
    ".",
    "\n",
    "-",
    " ",
    "W",
    "h",
    "e",
    "n",
    " ",
    "t",
    "h",
    "e",
    " ",
    "l",
    "o",
    "o",
    "p",
    " ",
    "e",
    "n",
    "d",
    "s",
    ",",
    " ",
    "`",
    "a",
    "`",
    " ",
    "i",
    "s",
    " ",
    "t",
    "h",
    "e",
    " ",
    "G",
    "C",
    "D",
    ".",
    "\n",
    "\n"
   ],
   "id": "4cf297e8"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "/// Return the greatest common divisor of nonnegative numbers, not both 0.\n",
    "public static int GreatestCommonDivisor(int a, int b)\n",
    "{\n",
    "    while (b != 0)\n",
    "    {\n",
    "        int r = a % b;\n",
    "        a = b;\n",
    "        b = r;\n",
    "    }\n",
    "    return a;\n",
    "}\n",
    "\n"
   ],
   "id": "7f0b1848"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}