{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "704d131c",
   "metadata": {},
   "source": [
    "(simple-if-statements)=\n",
    "\n",
    "# Conditional (`if`) Statements\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9863703",
   "metadata": {},
   "source": [
    "## Simple `if` Statements\n",
    "\n",
    "The `if` statement specifies a block of C# code to be executed if a condition is True.\n",
    "\n",
    "```none\n",
    "if (condition)\n",
    "{\n",
    "    // block of code to be executed if the condition is True\n",
    "}\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    ":::{note}\n",
    "Note that C# is case sensitive, so `If` is not the same as `if`. \"If\" or \"IF\" will\n",
    "generate an error.\n",
    ":::\n",
    "\n",
    "In `if` statements, the comparison operators (`>`, `<`, `>=`, `<=`, `==`, and `!=`) are used to evaluate the comparison expressions to derive a value of either `true` or `false` from the Boolean expression.\n",
    "\n",
    "The first step to learn about the `if` statements is to perform some tests on the operators\n",
    "in `csharprepl` such as:\n",
    "\n",
    "```csharp\n",
    "if (20 > 10)\n",
    "{\n",
    "Console.WriteLine(\"20 is greater than 10\");\n",
    "}\n",
    "```\n",
    "\n",
    "with the output:\n",
    "\n",
    "```none\n",
    "20 is greater than 10\n",
    "```\n",
    "\n",
    "When coding, we often play with variables rather than simple values. Observe the\n",
    "following code and test it in csharprepl to practice `if` statements with variables:\n",
    "\n",
    "```csharp\n",
    "int num1 = 20;\n",
    "int num2 = 18;\n",
    "if (num1 > num2)\n",
    "{\n",
    "Console.WriteLine(\"num1 is greater than num2\");\n",
    "}\n",
    "```\n",
    "\n",
    "Consider simple arithmetic comparisons that directly translate from math into C#.\n",
    "In csharprepl, enter:\n",
    "\n",
    "```csharp\n",
    "int num = 11;\n",
    "```\n",
    "\n",
    "Now think of which of these expressions below are true and which false,\n",
    "    and then enter each one into your csharp session to test:\n",
    "\n",
    "```csharp\n",
    "2 < 5\n",
    "3 > 7\n",
    "num > 10\n",
    "2*x < num\n",
    "```\n",
    "\n",
    "You see that the expressions evaluate to either `true` or `false`. These are the only possible *Boolean* values.\n",
    "\n",
    "Run the example program below. Try it at least twice, with inputs: 30 and then 55. As you can see, you get different results, depending on the input. The main code is:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b80f28f3",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "// namespace IntroCSCS\n",
    "\n",
    "class Chapter04\n",
    "{\n",
    "   static void Main(string[] args)\n",
    "   {\n",
    "      Weight();\n",
    "   }\n",
    "\n",
    "   public static void Weight()\n",
    "   {\n",
    "      Console.Write(\"How many pounds does your suitcase weigh? \");\n",
    "\n",
    "      double weight = double.Parse(Console.ReadLine());\n",
    "      \n",
    "      if (weight > 50)\n",
    "      {\n",
    "         Console.WriteLine(\"There is a $25 charge for luggage that heavy.\");\n",
    "      }\n",
    "      \n",
    "      Console.WriteLine(\"Thank you for your business.\");      \n",
    "   }\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf040d14",
   "metadata": {},
   "source": [
    "In this code, following line numbers as:\n",
    "\n",
    "- #5: The Main() method in the Chapter04 class called the Weight() method (line #10).\n",
    "- #16: the `if` statement in the Weight() method test the condition inside the parentheses.\n",
    "  - If the condition is true that the weight is greater than 50, then the code block #17-19 would run, printing that there will be a $25 charge.\n",
    "- #21: **No matter** whether the if statement (#14-17) runs or not, print the \"thank you\" message.\n",
    "\n",
    "You can see from this code that:\n",
    "\n",
    "1. the general C# syntax for a simple `if` statement is:\n",
    "\n",
    "``` none\n",
    "if (condition)\n",
    "{\n",
    "   // statement(s)\n",
    "}\n",
    "```\n",
    "\n",
    "2.  If the condition is `true`, then `execute` the statement(s) in braces. If the\n",
    "    condition is `not true`, then `skip` the statements in braces.\n",
    "\n",
    "3.  The `condition` is an `expression` that evaluates to either true or false,\n",
    "    of type **boolean**.\n",
    "\n",
    "4.  An `if` statement only affects the normal sequential order *inside* the `if`\n",
    "    statement itself (e.g., skipping the extra charge block when the condition is not true and still print the “thank you” line).\n",
    "\n",
    "Another code fragment of banking account transaction as an example:\n",
    "\n",
    "if (balance < 0)\n",
    "{\n",
    "  transfer = - balance;\n",
    "  // transfer enough from the backup account:\n",
    "  backupAccount = backupAccount - transfer;\n",
    "  balance = balance + transfer;\n",
    "}\n",
    "\n",
    "The assumption in the example above is that if an account goes negative, it is brought back to 0 by transferring money from a backup account. Note that the choice is between \n",
    "\n",
    "- **doing something** (if the condition is `true`) or \n",
    "- **nothing** (if the condition is `false`)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23f1728c",
   "metadata": {},
   "source": [
    "## `if-else` Statements: Two-Way Selection\n",
    "\n",
    "Since we can usually start analyzing a problem by coming up with two possibilities,\n",
    "it makes sense to add the alternative action to the code, which makes the `if-else` statements.\n",
    "\n",
    "The general C# {{ if_else }} syntax is:\n",
    "\n",
    "```csharp\n",
    "if (condition)\n",
    "{\n",
    "    // statement(s) for if-true;\n",
    "}\n",
    "else \n",
    "{\n",
    "    // statement(s) for if-false;\n",
    "}\n",
    "```\n",
    "\n",
    "Let us start by running the following example code (Clothes() method in Chapter04.cs).\n",
    "Try it at least twice, with inputs 50 and then 80.\n",
    "As you can see, you get different results, depending on the input.\n",
    "\n",
    "```{code-block} csharp\n",
    ":linenos:\n",
    "// namespace IntroCSCS\n",
    "// {\n",
    "\n",
    "class Chapter04\n",
    "{\n",
    "public static void Main(string[] args)\n",
    "{\n",
    "// Rolla();\n",
    "// Weight();\n",
    "   Clothes();\n",
    "}\n",
    "\n",
    "   public static void Clothes()\n",
    "   {\n",
    "      Console.Write(\"What is the temperature? \");\n",
    "      double temperature = double.Parse(Console.ReadLine());\n",
    "      if (temperature > 70)\n",
    "         {\n",
    "            Console.WriteLine(\"Wear shorts.\");\n",
    "         }\n",
    "      else\n",
    "         {\n",
    "            Console.WriteLine(\"Wear long pants.\");\n",
    "         }\n",
    "      Console.WriteLine(\"Get some exercise outside.\");\n",
    "   }\n",
    "\n",
    "}\n",
    "// }\n",
    "```\n",
    "\n",
    "You may see a warning in VS Code and when running the code as \n",
    "\n",
    "```text\n",
    "warning CS8604:\n",
    "Possible null reference argument for parameter 's' in 'double double.Parse(string s)'.\n",
    "```\n",
    "\n",
    "You can safely disregard the warning message. Basically, C# compiler produces warning at that line because argument of Parse is marked as \"non-nullable\" and the compiler determined that the parameter input you are passing to that call can be null at the point of call. For more information about this warning, see [C# language reference](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings#possible-null-assigned-to-a-nonnullable-reference).\n",
    "\n",
    "After running the code, you see that the `if-else` statement allows you to choose\n",
    "which of the two code paths to follow based on a Boolean expression.\n",
    "In an {{ if_else }} statement, an if statement is followed by an\n",
    "`else` statement that is only executed when the original `if` condition is *false*.\n",
    "Note that in an {{ if_else }} statement, `exactly one` of two possible code blocks in braces is executed.\n",
    "\n",
    "A final print line is also shown that is not indented, about getting exercise. The `if` and `else` clauses each only embed a *single* (possibly compound) statement as option, so the last statement is not part of the {{ if_else }} statement. It is beyond the {{ if_else }} statement. It is just a part of the normal `sequential` flow of statements and therefore will be executed as part of the flow.\n",
    "\n",
    ":::{admonition} Scope in Compound Statements\n",
    ":class: note\n",
    "\n",
    "Just like the local scope in method bodies, which happen to be enclosed in braces, making the function body a *compound statement*. In fact variables declared inside *any* compound statement have their scope restricted to *inside* that compound statement. As a result the following code makes no sense:\n",
    "\n",
    "```csharp\n",
    "static int BadBlockScope(int x)\n",
    "{\n",
    "    if (x < 100) {\n",
    "        int val = x + 2;\n",
    "    }\n",
    "    else {\n",
    "        int val = x - 2;\n",
    "    }\n",
    "    return val;\n",
    "}\n",
    "```\n",
    "\n",
    "The `if-else` statement is legal, but useless, because whichever compound statement gets executed, `val` ceases being defined after the closing brace of its compound statement, so the `val` in the return statement has not been declared or given a value. The code would generate a compiler error.\n",
    "\n",
    "If we want `val` to be used inside the braces and to make sense past the end of the compound statement, it cannot be declared inside the braces. Instead it must be declared before the compound statements that are parts of the `if-else` statement. A local variable in a function declared before a nested compound statement is still visible (in scope) *inside* that compound statement.\n",
    ":::"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eba10858",
   "metadata": {},
   "source": [
    "```{index} if-else-if\n",
    "```\n",
    "\n",
    "(multiple-tests)=\n",
    "\n",
    "## `else-if` Statements: Multi-Selection\n",
    "\n",
    "`if-else` statements allow us to construct two alternative paths.\n",
    "A single condition determines which path will be followed.\n",
    "We can build more complex conditionals using an else if clause, with which\n",
    "you can pick between 3 or more possibilities.\n",
    "These allow us to add additional conditions and code blocks, which\n",
    "facilitate more complex branching.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f96bcff2",
   "metadata": {},
   "source": [
    "## else-if Statements\n",
    "\n",
    "Comparing with the `if-else` statement, the `else if` statements specify\n",
    "a new condition to be tested if the first condition is False.\n",
    "\n",
    "The syntax for the `else-if` statement is:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "775ebcb0",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "if (condition1)\n",
    "{\n",
    "    // block of code to be executed if condition1 is True\n",
    "}\n",
    "else if (condition2)\n",
    "{\n",
    "    // block of code to be executed if the condition1 is False and condition2 is True\n",
    "}\n",
    "else\n",
    "{\n",
    "    // block of code to be executed if the condition1 is False and condition2 is False\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fdb4beb",
   "metadata": {},
   "source": [
    "\n",
    "Note that the logic of the flow terminate at `else`: The `else` block is the\n",
    "default block when all the conditions are False.\n",
    "\n",
    "An example of else-if statement is:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58b8a25c",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "int x = 10;\n",
    "int y = 20;\n",
    "\n",
    "if (x > y)\n",
    "{\n",
    "Console.WriteLine(\"x is greater than y\");\n",
    "}\n",
    "else if (x < y)\n",
    "{\n",
    "Console.WriteLine(\"x is less than y\");\n",
    "}\n",
    "else\n",
    "{\n",
    "Console.WriteLine(\"x and y are equal\");\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e75db12b",
   "metadata": {},
   "source": [
    "\n",
    "In the example above, the logic is that the three conditions can exhaust\n",
    "all possible alternatives. If the first condition is False, the next\n",
    "condition, in the else-if statement, will be tested. If the 2nd condition\n",
    "is also False, we then move on to the `else` statement, the **default**\n",
    "case, since condition1 and condition2 are both False.\n",
    "\n",
    "The code below is a variation of the Weight() method. Consider this\n",
    "fragment *without* a final `else`:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "add0898f",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "if (weight > 120) {\n",
    "    Console.WriteLine(\"Sorry, we can not take a suitcase that heavy.\");\n",
    "}\n",
    "else if (weight > 50) {\n",
    "    Console.WriteLine(\"There is a $25 charge for luggage that heavy.\");\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94395f4b",
   "metadata": {},
   "source": [
    "As with a simple if statement, the else clause is optional in else-if statements.\n",
    "This code above only prints one of two lines if there is a\n",
    "problem with the weight of the suitcase (> 50 or > 120). Nothing is printed if\n",
    "neither of the conditions is met, meaning the weight is not greater than 50."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a50b3fc",
   "metadata": {},
   "source": [
    "(else-if-cases)=\n",
    "\n",
    "## `else-if` Statements for Cases\n",
    "\n",
    "Consider the following code, LetterGrade() method, for converting a\n",
    "numerical grade to a letter grade, 'A', 'B', 'C', 'D' or 'F', where\n",
    "the cutoffs for 'A', 'B', 'C', and 'D' are 90, 80, 70, and 60\n",
    "respectively. We repetitively nests the if statements in\n",
    "the else clauses. The code structure is legal but semantically unsound if\n",
    "we consider the grade letters to be a flat structure:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "277f02b7",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "static char LetterGrade(double score)\n",
    "{\n",
    "    char letter;\n",
    "    if (score >= 90) {\n",
    "        letter = 'A';\n",
    "    }\n",
    "    else {   // grade must be B, C, D or F\n",
    "        if (score >= 80) {\n",
    "            letter = 'B';\n",
    "        }\n",
    "        else { // grade must be C, D or F\n",
    "            if (score >= 70) {\n",
    "            letter = 'C';\n",
    "            }\n",
    "            else {   // grade must D or F\n",
    "            if (score >= 60) {\n",
    "                letter = 'D';\n",
    "            }\n",
    "            else {\n",
    "                letter = 'F';\n",
    "            }\n",
    "            }   //end else D or F\n",
    "        }      // end of else C, D, or F\n",
    "    }         // end of else B, C, D or F\n",
    "    return letter;\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61533876",
   "metadata": {},
   "source": [
    "\n",
    "As seen in the LetterGrade() method above, the repeatedly increasing indentation\n",
    "with an `if` statement in the `else` clause can be annoying and\n",
    "distracting. Here is a preferred\n",
    "alternative in this situation, that avoids all this further\n",
    "indentation:\n",
    "Combine each `else` and following `if` onto the same line,\n",
    "and note that the `if` part after each else is just a *single*\n",
    "(possibly very complicated) statement. This allows the elimination of\n",
    "some of the braces and make the code more readable and logically clear:\n",
    "\n",
    "`if (`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa0a9db",
   "metadata": {},
   "source": [
    "*condition1*\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b009013d",
   "metadata": {},
   "source": [
    "`) {`\n",
    "\n",
    "statement-block-run-if-condition1-is-true;\n",
    "\n",
    "`}`\n",
    "\n",
    "`else if (`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc510f3a",
   "metadata": {},
   "source": [
    "*condition2*\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9994ed30",
   "metadata": {},
   "source": [
    "`) {`\n",
    "\n",
    "statement-block-run-if-condition2-is-the-first-true;\n",
    "\n",
    "`}`\n",
    "\n",
    "`else if (`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69d291b9",
   "metadata": {},
   "source": [
    "*condition3*\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7f122eb",
   "metadata": {},
   "source": [
    "`) {`\n",
    "\n",
    "statement-block-run-if-condition3-is-the-first-true;\n",
    "\n",
    "`}`\n",
    "\n",
    "// ...\n",
    "\n",
    "`else {    //`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44e05180",
   "metadata": {},
   "source": [
    "*no condition!*\n",
    "\n",
    "statement-block-run-if-no condition-is-true;\n",
    "\n",
    "`}`\n",
    "\n",
    "Note that *exactly one* of the statement blocks gets executed:\n",
    "\\- If some condition is true, the first block following a true condition is executed.\n",
    "\\- If no condition is true, the `else` block is executed.\n",
    "\n",
    "We can modify the LetterGrade() method into **LetterGrade2()**\n",
    "method using `else if`, which is more readable and semantically sound:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07981015",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "static char LetterGrade2(double score)\n",
    "{\n",
    "    char letter;\n",
    "    if (score >= 90) {\n",
    "        letter = 'A';\n",
    "    }\n",
    "    else if (score >= 80) {\n",
    "        letter = 'B';\n",
    "        }\n",
    "    else if (score >= 70) {\n",
    "        letter = 'C';\n",
    "    }\n",
    "    else if (score >= 60) {\n",
    "        letter = 'D';\n",
    "    }\n",
    "    else {\n",
    "        letter = 'F';\n",
    "    }\n",
    "    return letter;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b43f656d-7477-4abf-b396-dc4803ec5803",
   "metadata": {},
   "source": [
    "## Nested Conditionals\n",
    "\n",
    "The problem-solving scenario that you want to model may require\n",
    "complex branching behavior by combining conditionals and,\n",
    "in particular, by nesting conditionals. Namely, we can create more\n",
    "complex branching behavior by nesting conditional blocks.\n",
    "When more than one condition needs to be true and one of the\n",
    "condition is the sub-condition of parent condition, nested if can be used.\n",
    "In other words, when we want to further test certain condition\n",
    "when a condition tested true.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb8742e",
   "metadata": {},
   "source": [
    "## Nested `if` Statements\n",
    "\n",
    "The syntax for a nested if statement is as follows:\n",
    "\n",
    "    if( boolean_expression 1)\n",
    "    {\n",
    "       /* Executes when the boolean expression 1 is true */\n",
    "       if(boolean_expression 2)\n",
    "       {\n",
    "          /* Executes when the boolean expression 2 is true */\n",
    "       }\n",
    "    }\n",
    "\n",
    "As an example, consider the following code where two separate but\n",
    "related ideas are tested using two independent if statements:\n",
    "\n",
    "    int num = 7;\n",
    "\n",
    "    if (num > 0)\n",
    "    {\n",
    "       Console.WriteLine(\"POSITIVE\");\n",
    "    }\n",
    "\n",
    "    if (num % 2 == 0)\n",
    "    {\n",
    "       Console.WriteLine(\"EVEN\");\n",
    "    }\n",
    "\n",
    "We find that the output is POSITIVE, even though 7 is odd and so\n",
    "nothing should be printed in the 2nd if statement. This code\n",
    "doesn’t work as desired if we only want to test for evenness only\n",
    "when we already know the number is positive. We can enable this\n",
    "behavior by putting the second conditional inside the first as:\n",
    "\n",
    "    int num = 7;\n",
    "\n",
    "    if (num > 0)\n",
    "    {\n",
    "       Console.WriteLine(\"POSITIVE\");\n",
    "\n",
    "       if (num % 2 == 0)\n",
    "       {\n",
    "          Console.WriteLine(\"EVEN\");\n",
    "       }\n",
    "    }\n",
    "\n",
    "Notice that when we put one conditional inside another, the body of the\n",
    "nested conditional is indented by two tabs rather than one. This convention\n",
    "provides an easy, visual way to determine which code is part of which conditional.\n",
    "\n",
    "Note that nested if statements can also contain an else statement.\n",
    "When working with nested statements, the `else clause` belongs to\n",
    "the last unpaired if. You can only use an else when you have an if.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e0d68f7",
   "metadata": {
    "language_info": {
     "name": "polyglot-notebook"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    },
    "vscode": {
     "languageId": "polyglot-notebook"
    }
   },
   "outputs": [],
   "source": [
    "int num = 7;\n",
    "\n",
    "if(num < 9)\n",
    "{\n",
    "if (num % 2 == 0)\n",
    "{\n",
    "Console.WriteLine(\"EVEN\");\n",
    "}\n",
    "\n",
    "else\n",
    "{\n",
    "Console.WriteLine(\"ODD\");\n",
    "}\n",
    "}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6758d913",
   "metadata": {},
   "source": [
    "## Nested `if-else` Statements\n",
    "\n",
    "    Often you want to distinguish between more than two distinct cases,\n",
    "    but conditions only have two possible results, `true` or `false`,\n",
    "    so the only direct choice is between two options. If there are\n",
    "    more than two choices, a single test may only reduce the\n",
    "    possibilities, but further tests can reduce the possibilities\n",
    "    further and further until the possibilities are exhausted. Since almost\n",
    "    any kind of statement can be placed in the sub-statements in\n",
    "    an {{ if_else }} statement, one choice is a further `if` or {{ if_else }} statement.\n",
    "\n",
    "    For instance, consider the LetterGrade() method that you have seen earlier.\n",
    "    One way to write the method would be to test for one grade at a time, and\n",
    "    resolve all the remaining possibilities inside the next `else` clause.\n",
    "\n",
    "    If we do this consistently with our indentation conventions so far:\n",
    "\n",
    "static char LetterGrade(double score)\n",
    "{\n",
    "char letter;\n",
    "if (score \\>= 90) {\n",
    "letter = ‘A’;\n",
    "}\n",
    "else { // grade must be B, C, D or F\n",
    "if (score \\>= 80) {\n",
    "letter = ‘B’;\n",
    "}\n",
    "else { // grade must be C, D or F\n",
    "if (score \\>= 70) {\n",
    "letter = ‘C’;\n",
    "}\n",
    "else { // grade must D or F\n",
    "if (score \\>= 60) {\n",
    "letter = ‘D’;\n",
    "}\n",
    "else {\n",
    "letter = ‘F’;\n",
    "}\n",
    "} //end else D or F\n",
    "} // end of else C, D, or F\n",
    "} // end of else B, C, D or F\n",
    "return letter;\n",
    "}\n",
    "\\`\\`\\`\n",
    "\n",
    "When working with else statements in nested conditionals, remember that\n",
    "the else is paired with the last if that doesn’t have already have an else.\n",
    "In the example above, the else statement in line 10 belongs to the if in line 5.\n",
    "else and else if rules apply the same way within nested conditionals as in un-nested ones.\n",
    "\n",
    "In {{ if_else }} statements, the sub-statements (the if-true and if-false clauses)\n",
    "are quite arbitrary statements. There can be more `if` or\n",
    "{{ if_else }} sub-statements.\n",
    "\n",
    "In method LetterGrade() above, we place an {{ if_else }} statement as the `else`\n",
    "clause, and repeating this pattern, to repeatedly test for one more case,\n",
    "stopping when the **first true condition if reached**.\n",
    "\n",
    "% To choose one case\n",
    "\n",
    "% from multiple cases, each condition separates one case terminal\n",
    "\n",
    "% case from all the remaining untested cases."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "footnotes",
   "metadata": {},
   "source": [
    "```{rubric} Footnotes\n",
    "```\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
