{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "43013eb8",
   "metadata": {},
   "source": [
    "# Switch Statement\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8bbe6a7",
   "metadata": {},
   "source": "## Switch Statement\n\nThe switch statement is used to select one of many code blocks (\"cases\")\nto be executed. Logically, it resembles the use of a series of `else-if` clauses.\n\nThe syntax for `switch` statement is as follows:\n\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "switch(expression)\n",
    "{\n",
    "case x:\n",
    "    // code block\n",
    "    break;\n",
    "case y:\n",
    "    // code block\n",
    "    break;\n",
    "default:\n",
    "    // code block\n",
    "    break;\n",
    "}\n"
   ],
   "id": "a60bfde0"
  },
  {
   "cell_type": "markdown",
   "id": "18f54e41",
   "metadata": {},
   "source": [
    "\n",
    "Note that:\n",
    "\\- The switch expression is evaluated once.\n",
    "\\- The value of the expression is **compared** with the values of each case.\n",
    "\\- If there is a **match** between the `expression` value and the `case` value, the associated block of code is executed.\n",
    "\\- The `break` and `default` keywords will be described later in this chapter.\n",
    "\n",
    "As an example, we can use the weekday number to figure out weekday name:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int day = 4;\n",
    "switch (day)\n",
    "{\n",
    "case 1:\n",
    "    Console.WriteLine(\"Monday\");\n",
    "    break;\n",
    "case 2:\n",
    "    Console.WriteLine(\"Tuesday\");\n",
    "    break;\n",
    "case 3:\n",
    "    Console.WriteLine(\"Wednesday\");\n",
    "    break;\n",
    "case 4:\n",
    "    Console.WriteLine(\"Thursday\");\n",
    "    break;\n",
    "case 5:\n",
    "    Console.WriteLine(\"Friday\");\n",
    "    break;\n",
    "case 6:\n",
    "    Console.WriteLine(\"Saturday\");\n",
    "    break;\n",
    "case 7:\n",
    "    Console.WriteLine(\"Sunday\");\n",
    "    break;\n",
    "}\n"
   ],
   "id": "ba1cbf47"
  },
  {
   "cell_type": "markdown",
   "id": "cda2eecd",
   "metadata": {},
   "source": [
    "\n",
    "The outcome of this code will be Thursday. The code will stop\n",
    "executing because of the `break` keyword after the print line.\n",
    "Also, there is not `default case` in this code block.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7e4a0bf",
   "metadata": {},
   "source": "## The `default` Keyword\n\nThe default keyword is optional and it specifies some code to run\nif there is no case match:\n\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int day = 4;\n",
    "switch (day)\n",
    "{\n",
    "case 6:\n",
    "    Console.WriteLine(\"Today is Saturday.\");\n",
    "    break;\n",
    "case 7:\n",
    "    Console.WriteLine(\"Today is Sunday.\");\n",
    "    break;\n",
    "default:\n",
    "    Console.WriteLine(\"Looking forward to the Weekend.\");\n",
    "    break;\n",
    "}\n",
    "// Outputs \"Looking forward to the Weekend.\"\n"
   ],
   "id": "d71797c6"
  },
  {
   "cell_type": "markdown",
   "id": "c46b8aa1",
   "metadata": {},
   "source": "## The `break` Keyword\n\nWhen a match is found in a switch statement, the job is done,\nit's time for a break. There is no need for more testing and matching\ncases. When C# reaches a `break` keyword, it breaks out of the\nswitch block. This will stop the further sequential execution of\ncode and case testing inside the block.\n\nA `break` can save execution time because it ignores\nthe execution of all the rest of the code in the switch block. But\nmore importantly, a `break` servers as a terminator of the\ncontrol flow. That also means that you may choose to continue\nmatching cases after one is matched by not placing break at the\nend of the case block.\n\n% Dangerous Semicolon\n\n% ~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n% Regular statements must end with a semicolon.\n\n% It turns out that the semicolon is all you need to have a legal statement::\n\n% ;\n\n% We will see places that it is useful, but\n\n% meanwhile it can cause errors: You may be hard pressed to\n\n% remember to put semicolons at the end of all your statements, and in response you may\n\n% get compulsive about adding them at the end of statement\n\n% lines.  Be careful NOT to put one at the end of a method heading or\n\n% an ``if`` condition::\n\n% if ( x < 0); // WRONG PROBABLY!\n\n% Console.WriteLine(x);\n\n% This code is deadly, since it compiles and is almost surely\n\n% *not* what you mean.\n\n% Remember indentation and newlines are only significant for humans. The\n\n% two lines above are equivalent to::\n\n% if ( x < 0)\n\n% ;  // Do nothing as statement when the condition is true\n\n% Console.WriteLine(x); // past if statement - do it always\n\n% (Whenever you do need an empty statement, you are encouraged to put the\n\n% semicolon all by itself on a line, as above.)\n\n% If you always put an open brace *directly* after the condition in an ``if`` statement,\n\n% you will not make this error::\n\n% if ( x < 0) {\n\n% Console.WriteLine(x);\n\n% }\n\n% Then even if you were to add a semicolon::\n\n% if ( x < 0) { ;\n\n% Console.WriteLine(x);\n\n% }\n\n% it would be a waste of a keystroke, but it would just be the first (empty) statement\n\n% inside the block, and the writing would still follow:\n\n% The extra semicolon would have no effect.\n\n% The corresponding error at the end of a method heading will at least\n\n% generate a compiler error, though it may appear cryptic::\n\n% static void badSemicolon(int x);\n\n% {\n\n% x = x + 2;\n\n% // ...\n\n% This is another easy one to make and *miss* - just one innocent semicolon.\n\n% .. index:: pitfall; dangling else;\n\n% dangling else pitfall\n\n% if-else; pitfall\n\n% .. _match_wrong_if:\n\n% Match Wrong ``if`` With ``else``\n\n% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n% If you do not consistently put the substatements for the true\n\n% and false choices inside braces, you can run into problems from\n\n% the fact that the else part of an if statement is *optional*.\n\n% Even if you use braces consistently,\n\n% you may well need to read code that does not place\n\n% braces around single statements. If C# understood indentation as\n\n% in the recommended formatting style (or as required in Python),\n\n% the following would be OK::\n\n% if (x > 0)\n\n% if (y > 0)\n\n% Console.WriteLine(\"positive x and y\");\n\n% else\n\n% Console.WriteLine(\"x not positive, untested y\");\n\n% Unfortunately placing the ``else`` under the first ``if`` is not enough to make\n\n% them go together (remember the C# compiler ignores extra whitespace). The\n\n% following is equivalent to the compiler, with the else apparently going\n\n% with the second if::\n\n% if (x > 0)\n\n% if (y > 0)\n\n% Console.WriteLine(\"positive x and y\");\n\n% else\n\n% Console.WriteLine(\"x not positive, untested y\");\n\n% The compiler is consistent with the latter visual pattern: an ``else`` goes\n\n% with the most *recent* ``if`` that could still take an ``else``.\n\n% Hence if ``x`` is 3\n\n% and ``y`` is -2, the ``else`` part is executed and the statement printed is\n\n% incorrect: in this code\n\n% the else clause is only executed when ``x`` is positive and\n\n% ``y`` (*is*\n\n% tested and) is not positive.\n\n% If you put braces everywhere to reinforce\n\n% your indentation, as we suggest, or if you only add the following\n\n% one set of braces around the inner if statement::\n\n% if (x > 0) {\n\n% if (y > 0)\n\n% Console.WriteLine(\"positive x and y\");\n\n% }\n\n% else\n\n% Console.WriteLine(\"x not positive, untested y\");\n\n% then the braces enclosing the inner ``if`` statement make it impossible for\n\n% the inner  ``if`` to continue on to an optional ``else`` part.\n\n% The ``else`` must go\n\n% with the first ``if``. Now when the ``else`` part is reached, the statement\n\n% printed will be true: ``x`` is not positive, and the test of ``y`` was skipped.\n\n% .. index::\n\n% pitfall; need braces for if\n\n% if; need braces\n\n% braces needed with if\n\n% .. _missing-braces:\n\n% Missing Braces\n\n% ~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n% Another place you can fool yourself with nice indenting style is\n\n% something like this.  Suppose we start with a perfectly reasonable ::\n\n% if (x > 0)\n\n% Console.WriteLine(\"x is: positive\");\n\n% We may decide to avoid the braces, since there *is* just one statement\n\n% that we want as the if-true part, but if we later decide\n\n% that we want this on two lines\n\n% and change it to ::\n\n% if (x > 0)\n\n% Console.WriteLine(\"x is:\");\n\n% Console.WriteLine(\"  positive\");\n\n% We are not going to get the behavior we want.\n\n% The word \"positive\" will *always* be printed.\n\n% If we had first taken a bit more effort originally to write ::\n\n% if (x > 0) {\n\n% Console.WriteLine(\"x is: positive\");\n\n% }\n\n% then we could have split successfully into  ::\n\n% if (x > 0) {\n\n% Console.WriteLine(\"x is:\");\n\n% Console.WriteLine(\"  positive\");\n\n% }\n\n% This way we do not have to keep worrying about this question when we revise:\n\n% \"Have I switched to multiple lines after the ``if``\n\n% and need to introduce braces?\"\n\n% The last two of the pitfalls mentioned in this section are fixed by consistent\n\n% use of braces in the sub-statements of ``if`` statements.  They fix the ``;``\n\n% after if-condition problem only if the open brace comes right after\n\n% the condition, but you still get a nasty error if you put in a semicolon\n\n% between the condition and opening brace."
  },
  {
   "cell_type": "markdown",
   "id": "footnotes",
   "metadata": {},
   "source": "```{rubric} Footnotes\n```\n"
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}